Objective-c – TabBarController NAvigationController and UIViewController

iphoneobjective c

I have the next question:

In my project I have the next:

UItabbarController

….Some UINAvigationControllers….

*(1) UINavigationController

  UIViewController (UItableView) - When select one row it goes to...(by push) to:

              UIViewController (UItableView) - And here the same than before, for each row i open a new tableview....

My problem is when i click in the tab bar item, I see the viewController view like last time that i saw this, and no reload to the *(1) first view another time( like i would like)

Where I need to write sth for each time that i click in a tab bar item i reload the first view of this tab bar item.

PD: I have the call: [theTableView reloadData]; in method "viewWillAppear".

The thing I'm doing is:
In my navigation Controller I have a View Controller (like tableview) and when i click in one row, in the "didSelectRowAtIndexPath" method I create another View Controller calling "myController" and i push this element like this ( [[self navigationController] pushViewController:myController animated:YES];)
And this for each time i click in one row in the next tables.

Then I think the problem is not to reload the table view in the method viewWillAppear, it's to take out from the screen the next views controller that I inserted to the root one.

I'm rigth?

IN RESUME:

My app has the next:

  • Tab bar to move between screens
  • Navigation inside each tab bar (as far as you want), why? because all the tabBarItems show Tables, and if you click in one row you open another table,…..

My problem then is that I would like to come back to the 1st Main table when i click in the tab bar. For the moment the app doesn't do this, it continue in the scree(table view) that was the last visit in this tab. (Is not completely true, because if i click two time, Yes, it comes back but don't enter in the "viewWillLoad" or "didSelectViewController" methods, because i made NSLogs and it doesn't show them).

The sketche can be this:

AppDelegate -> WelcomeScreen ->VideosTableViewController ->RElatedVideosTableViewController -> ….. ….. ….

The 1st thing is to show the Welcome screen (not so important, only some buttons) and in this class i have the TabBArController initialized with "localViewControllersArray" that is a NSMutableArray of NavigationControllers each initialized with one ViewController .

Then when i press one of the buttons in this welcome Screen i sho the tab bar Controller (Shows the VideosTableViewController)

In the next step, when I click in one row, in "DidSelectRowAtIndexPath" I create a RElatedVideosTableViewController, and I push this by "[[self navigationController] push….: "The relatedvideo table view i create" animated:YES];

AND I ALSO HAVE:

Add: UITabBarControllerDelegate
Add:

  • (void)tabBarController:(UITabBarController*)tabBarController
    didEndCustomizingViewControllers:
    (NSArray*)viewControllers
    changed:(BOOL)changed { }

  • (void)tabBarController:(UITabBarController*)tabBarController
    didSelectViewController:(UIViewController*)viewController
    { if ([viewController
    isKindOfClass:[UINavigationController
    class]])
    { [(UINavigationController *)viewController popToRootViewController:NO];
    [theTableView reloadData];
    NSLog(@"RELOAD");
    } }

And at the initialization of the class:
[super.tabBarController setDelegate:self];

But in the console I don't see the NSLog I'm making then is not going in this method.

Best Answer

Make your app delegate the tab bar controller's delegate, either in Interface Builder or in code:

- (void)applicationDidFinishLaunching
{
    ...
    self.tabBarController.delegate = self;
}

Then, when the tab bar switches to a different view, you get notified, at which point you pop to the root of the selected nav controller thus:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
      if ([viewController isKindOfClass:[UINavigationController class]])
      {
              [(UINavigationController *)viewController popToRootViewController:NO];
      }
}

Each view controller should have its own table view, so I don't know what you are trying to do by the reload.

Related Topic