Search code examples
iosuiviewcontrolleruinavigationcontrolleruitabbarcontroller

self.navigationController.tabBarController.selectedIndex not working


I have a UITabBarController which containing 4 different UIViewControllers.

On first tab there is a UINavigationViewController which contains its child UIViewController. without tapping the tab bar, i want to take user on second tab. For this I tried:

self.navigationController.tabBarController.selectedIndex = 1;

But it's not working.

ignore any mistake i am a newbie.

Thank you.


Solution

  • Do the following, before trying to change tab:

    UITabBarController *tab = self.tabBarController;
    
    if (tab) {
       NSLog(@"I have a tab bar");
       [self.tabBarController setSelectedIndex: 1];
    }
    else {
       NSLog(@"I don't have one");
    }