reset previous navigation controller in tab bar application
-
28-09-2019 - |
Question
I have a tab bar application with a navigation controller and view controllers inside it. This is how it's laid out.
Tab Bar 1
- Navigation Controller
-- Root View Controller
-- Other view controller
Tab Bar 2
- Navigation Controller
-- Root View Controller
What I want to do is when I navigate to the Other view controller in Tab Bar 1, then I switch to Tab Bar 2, I want the Tab Bar 1 to return to its Root View Controller.
The behavior I'm looking for is similar to the iPod App. Any help is appreciated. Thanks!
Solution
It sounds like you've started out with the 'Tab Bar Application' template so most likely your app delegate is also a delegate for your UITabBarController. UITabBarControllerDelegate has this method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
In this method you would check to see if 'viewController' is your first navigation controller and if it is, set the navigation controllers viewControllers property to an array of the child view controllers with root view controller at index 0.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (viewController == firstNavController)
viewController.viewControllers = [NSArray arrayWithObjects:rootViewController, otherViewController, nil];
}