objective-ciostabbar

Open the app in a specific tab in tabbar


I have an app with a TabBar with 5 tabs (regular tab bar with no custom class). When I start the app the left tab is opened. I want it to open the middle one first. I've tried putting

[self.tabBarController setSelectedIndex:3];

in the ViewDidLoad of the ViewController that is first opened but the tab isn't switching. I can see it's highlighted but not selected. If I put the code above under viewWillAppear it will be selected on first run but when I'll select the left tab from time to time it will jump to the middle one.

Also tried this without success:

DetailsViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailsViewController"];
[self.navigationController pushViewController:vc animated:true];    

What Am I doing wrong?


Solution

  • This is the only solution that worked for me. in appdelegate:

    UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController; 
    [tabBar setSelectedIndex:2];