Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 212
Segev Avatar asked Dec 20 '12 08:12

Segev


2 Answers

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

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController; 
[tabBar setSelectedIndex:2];
like image 197
Segev Avatar answered Nov 17 '22 00:11

Segev


In storyboard set custom class to the tabbarController and in that custom class

-(void) viewDidLoad
{
    [self.tabBarController setSelectedIndex:3];
}
like image 1
junaidsidhu Avatar answered Nov 16 '22 22:11

junaidsidhu