Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change initial tab bar selection programmatically

Having problems changing the initial tab bar selection on an application (i.e. the middle tab is selected on app launch rather than the leftmost tab). The app uses storyboards and the tab bar controller was added later on in development via the storyboard method.

tabBarController.selectedIndex = 1;

the above code doesn't work (because I don't have a custom view controller connected to my tab bar, just the default UITabBarController):

Done some googling and looked at many different resources and haven't quite found a solution for an app that wasn't initially created using Apple's template Tab Bar Application.

like image 275
LazerLex Avatar asked May 03 '12 23:05

LazerLex


2 Answers

Since this is the initial view controller, and is not a subclass, you need to set this in your appDelegate.

In AppDelegate.m, add the following to your application:didFinishLaunchingWithOptions: method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    // Select the left-most tab of our initial tab bar controller:
    UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
    tabBar.selectedIndex = 0;
    return YES;
}
like image 75
lnafziger Avatar answered Oct 19 '22 22:10

lnafziger


tabBar setSelectedItem: try using this in your viewDidLoad

like image 30
Mike Z Avatar answered Oct 19 '22 20:10

Mike Z