2
votes

When you have a UINavigationController integrated into one of the Tabs of a UITabBarController, the UITabBarController remembers where the user was last time the tab was used. Is it possible to have the UITabBarController forget and always start the tab as if it was the first time?

f.e. structure is a follows

  • Tab1
    • View1
    • View2
  • Tab2
    • View1

The user starts at Tab1/View1, then he navigates to Tab1/View2. He changes to Tab2/View1 and then presses Tab1:

  • Current behavior: he appears at Tab1/View2
  • Wanted behaviour: he appears at Tab1/View1
2
Look into this I've already posted hope you are asking about the same thing stackoverflow.com/questions/14479243/…Exploring

2 Answers

3
votes

Try like below it will help you

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    for(int i = 0; i < tabBarController.viewControllers.count; i++) {
        if(tabBarController.selectedIndex != i && [[tabBarController.viewControllers objectAtIndex:i] isKindOfClass:[UINavigationController class]])
            [[tabBarController.viewControllers objectAtIndex:i] popToRootViewControllerAnimated:NO];
    }
}
1
votes

Have a look at the UITabBar delegate protocole.

Then use tabBarController:didSelectViewController: that way :

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

    if (viewController != tabBarItem1) {
        [self.navigationControllerInTab1 popToRootViewControllerAnimated:NO];
    }
}

That way, when you leave the first tab item, it will pop the NavigationController to the rootViewController.