0

When i press certain Tab it's not going to its rootviewcontroller,

When user changes a tab, for the selected tab I want to push it to its top level controller.

I have implement this method but not works,

-(void)tabBarController:(UITabBarController *) tabBarController didSelectViewController : (UIViewController *)viewController
{
   [viewController.navigationController popToRootViewControllerAnimated:NO];
}

What's wrong with this?

How can i do that?

Ankit Chauhan
  • 2,375
  • 6
  • 25
  • 37

1 Answers1

2

Assuming the method gets called (if not you should set the UITabBarController delegate), you are probably receiving the UINavigationController (which is a subclass of UIViewController) as viewController, you can check by logging it:

-(void)tabBarController:(UITabBarController *) tabBarController didSelectViewController : (UIViewController *)viewController
{
   NSLog(@"didSelect %@", viewController);
   [viewController.navigationController popToRootViewControllerAnimated:NO];
}

if that is the case, viewController.navigationController will probably be nil, you should be doing:

-(void)tabBarController:(UITabBarController *) tabBarController didSelectViewController : (UIViewController *)viewController
{   
    if ([viewController isKindOfClass:[UINavigationController class]])
        [(UINavigationController*)viewController popToRootViewControllerAnimated:NO];
}
jbat100
  • 16,757
  • 4
  • 45
  • 70