Search code examples
objective-ciphone-sdk-3.0ios4

How do I integrate popToRootViewControllerAnimated with my tabs?


I am trying to make one of my tab buttons go to the root using popToRootViewControllerAnimated. My question is: where do I put this code for it to work? I have my tabs created through Interface Builder... do they have to be hard coded for this to work?

Here is the code that I'm looking to use:

[self.navigationController popToRootViewControllerAnimated:YES];

New code in AppDelegate:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (viewController = HomeViewController) {
        [HomeViewController popToRootViewControllerAnimated:NO];
    }
}

Solution

  • Adam - I ended up ditching the subclass idea even though it worked, as there is a much easier method.

    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
        if ([viewController isKindOfClass:[UINavigationController class]]) {
            [(UINavigationController*)viewController popToRootViewControllerAnimated:YES];
        }
    }
    

    This is the code required. I've uploaded this sample project to play around with. The main points are

    • the UITabBarController delegate must be set to the App Delegate.
    • the App Delegate must implement the <UITabBarControllerDelegate> protocol.
    • the App Delegate must implement the code above.

    The sample project also shows one way to selectively choose which navigation controllers this occurs with.