Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set selected tab in UITabBarController using StoryBoard?

Tags:

How can I switch to some tab in UITabBarController using StoryBoard? I have tried the code below but without success (the tab is not selected):

self.tabBar.selectedIndex = 3; 

Honestly I used nib files without StoryBoard and this code above worked fine in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions           :(NSDictionary *)launchOptions 

but now I can't set the tab programatically. Maybe there is another problem that is not connected to selecting the tab. How can I switch tabs?

like image 895
Matrosov Oleksandr Avatar asked Jul 29 '13 08:07

Matrosov Oleksandr


People also ask

How do I add items to my tab bar controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.


2 Answers

Grab your instance of UITabBarController then set the selectedViewController property:

yourTabBarController.selectedViewController=[yourTabBarController.viewControllers objectAtIndex:3];//or whichever index you want 
like image 63
Tommy Devoy Avatar answered Sep 20 '22 23:09

Tommy Devoy


Alexander, I think your problem is getting correct instance of your tab bar. If your tab bar is your root view controller, then you can do it like this in your appdelegate if didFinishLoading method:

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

Give it a try and tell me the result please.

like image 36
Yanchi Avatar answered Sep 20 '22 23:09

Yanchi