views:

352

answers:

2

I have four UINavigationControllers assigned each to a tab in a UITabBarController. Each UINavigationController manages a UIViewController, which may itself branch out into other UIViewControllers below it hierarchally.

My question is, in a case in which a user, under one tab, has navigated to a UIViewController that hierarchally BELOW the main UIViewController managed by the UINavigationController, and then the user pushes a different tab, and then goes back to the original tab, HOW can I make it so that the user is presented with the main UIViewController managed by the UINavigation controller? and not the page where he left off?

UITabBarController is set up in IB

A: 

Implement the UITabBarControllerDelegate protocol and pop to the root controller whenever your delegate is notified that the user has selected a different tab.

Something like:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [viewController.navigationController popToRootViewControllerAnimated:YES];
}

The code above should pop to the root controller of any navigation controller that is the main view controller of the newly selected tab. You could try implementing the protocol in your application delegate. And do not forget to actually assign the app delegate as the tab bar controller's delegate.

Ole Begemann
could you be more specific? All of my UINavigationControllers are not programmatic but are in IB only. Do I need to subclass them?
RexOnRoids
No just use self.navigationController
Corey Floyd
I have added some code and specifics to my answer. Note that I haven't tried it myself, but it should work along these lines.
Ole Begemann
+1  A: 

didn't work for me originally, then later on I noticed that all my viecontrollers for different tabs are actually UINavigationControllers

and thus I modified the above code a little bit as follows and it worked:

[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
Vibhor Goyal