0

I've got 5 views in my tabBarController and all of them are embedded in a separate navigationcontroller, i.e. every view has it's own navigationcontroller. I did this to make it easier to push segues, I know it's probably not the best solution but it works fine. Now to my question:

I'm trying to set the initial view with the following code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    tabBarController.selectedIndex = 2;
    return YES;
}

However it's not working at all, the app simply starts and index 0 (the left most view).

I've searched through thorough threads like this and tried many different ways to solve this without any success...

Closest I got was when I in MainStoryboard_iPhone.storyboard checked the box "Is initial view controller" in the viewcontroller I want to start with. This way the I got the correct starting viewcontroller but the tabbar wasn't showing.

Community
  • 1
  • 1
Mathias
  • 233
  • 3
  • 17
  • Try logging your tabBarController to make sure its not NULL, what does this return NSLog(TBC : %@, tabBarController); ? Add this log statement in your appDelegate didFinishLaunching: method – Dhruv Goel Jan 30 '13 at 20:50
  • Well you know you stuff sir, it returns "tabgaze[46885:c07] TBC : (null)". Does this mean I have to initialize the tabBarController and programmatically add the 5 viewcontrollers? It feels like everything is being intitiated twice that way since I'm using storyboard? Perhaps I'm wrong. – Mathias Jan 30 '13 at 20:54

1 Answers1

1

Since you're using storyboard, do this : 1) Give your tabBarController a storyboard identifier (say tbc); 2) In your appDelegate DidFinishLaunching, do this ::

UITabBarController *tbc = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"tbc"];

[tbc setSelectedIndex:1];
[self.window setRootViewController:tbc];
[self.window makeKeyAndVisible];

return YES;

PS : This is just one of many ways to make it work

Dhruv Goel
  • 2,715
  • 1
  • 17
  • 17
  • Worked like a charm, thank you very much! If anyone runs into the same problem I just realized that the "Identifier" in storyboard in Xcode 4.5 is called "Storyboard ID". – Mathias Jan 30 '13 at 21:06