1
votes

My app starts with a navigation controller which opens a UIViewController. This screen works as a login page.

On login, I open a UITabBarController like this:

  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    UITabBarController *obj=[storyboard instantiateViewControllerWithIdentifier:@"MainTab"];
    self.navigationController.navigationBarHidden=YES;
    [self.navigationController pushViewController:obj animated:YES];

Inside my tab bar controller, I want when clicking a button to switch tab programmatically. I tried the following 3, neither of them worked. Code is place inside a method, which is invoked when the button is clicked.

For the first 2, the tab didn't change - still my initial tab is highlighted and the correct view controller is not shown. For the last one, app crashes.

1st :

  self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:1];

2nd :

[self.parentViewController.tabBarController setSelectedIndex:1];

3rd:

UITabBarController *MyTabController = (UITabBarController *)((AppDelegate*) [[UIApplication sharedApplication] delegate]).window.rootViewController;
[MyTabController setSelectedIndex:1];

What am I missing?

1

1 Answers

0
votes

for a tabBar inside a navigation controller...What am I missing?

One thing you're missing is the order of containment that's allowed for view controllers. Specifically, you can put a navigation controller inside a tab controller, but not the other way around.