Daniel Klöck
Daniel Klöck

Reputation: 21137

make UITabBar forget last state

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

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

Upvotes: 2

Views: 94

Answers (2)

Narayana Rao Routhu
Narayana Rao Routhu

Reputation: 6323

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];
    }
}

Upvotes: 3

rdurand
rdurand

Reputation: 7410

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.

Upvotes: 1

Related Questions