Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 941
jamone Avatar asked Dec 16 '11 20:12

jamone


1 Answers

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.

like image 146
Mark Adams Avatar answered Oct 02 '22 03:10

Mark Adams