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?
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.
Grab your instance of UITabBarController
then set the selectedViewController
property:
yourTabBarController.selectedViewController=[yourTabBarController.viewControllers objectAtIndex:3];//or whichever index you want
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With