Pop to root view when tab is selected

John S picture John S · Oct 4, 2011 · Viewed 19.3k times · Source

I've been having some trouble with something I thought might be easy. I have a table in my root view controller, when a row is selected I push a new view and from there I go to another tab.

My question is how do I make sure that as soon as the user taps the first tab the navigation controller will pop to root?

Answer

ios developer picture ios developer · Sep 13, 2012

Following delegate is called while each tab is selected on tabbar.

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

Put following code inside this delegate method.

if ([viewController isKindOfClass:[UINavigationController class]]) 
    {
        [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
    }

its working fine on my app.