storyboard set rootViewController of NavController to tableView but on app launch show other view

17,479

Unfortunately UIStoryboard isn't able to manipulate a UINavigationController hierarchy in a visual way. In your case, you need to establish the hierarchy programmatically in your application delegate. Luckily, because you are storyboarding, your app delegate already contains a reference to this navigation controller.

When storyboarding, the so called "Initial View Controller" will be wired up to the rootViewController property on the application's instance of UIWindow by the time -applicationDidFinishLaunchingWithOptions: is messaged.

- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)options
{
    UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
    MenuViewController *menu = [navController.storyboard instantiateViewControllerWithIdentifier:@"MenuController"];
    CustomViewController *custom = [navController.storyboard instantiateViewControllerWithIdentifier:@"CustomController"];

    // First item in array is bottom of stack, last item is top.
    navController.viewControllers = [NSArray arrayWithObjects:menu, custom, nil];

    [self.window makeKeyAndVisible];

    return YES;
}

I realize this isn't ideal, but if you want to stay in the land of storyboards, I'm afraid this is the only way.

Share:
17,479
jamone
Author by

jamone

iOS and macOS app developer.

Updated on June 04, 2022

Comments

  • jamone
    jamone about 2 years

    I have a basic navigation stack: NavController->UITableViewController (as rootViewController in the NavController) -> menu items with the main option being a custom viewController. I want my app to launch with the main custom view controller as the current view on the navigationController's stack, and the back button to go to the Main Menu. Is there some way using storyboarding to setup the stack this way and yet on launch show the custom view first?

    I'm trying to do this in storyboarding as much as possible. I know I could go in the appDelegate and on appDidFinishLaunching... push the custom viewController into the navController but that just seems bad because then in my appDelegate I have to reference the navController and the custom viewController.

  • jamone
    jamone over 12 years
    Well that's still cleaner then they way I would have done it. Thanks.