0

I looked over the suggested list, and didn't see anything that seemed to fit my dilemma.

I am writing my first storyboard project. I have a UITabBarController that manages 4 tabs. Each tab has a UINavigationController as its root, and a number of views in each.

One of the navigation stacks is actually a "ring" of 3 view controllers. I use a custom segue to replace the usual "pile-on" of a navigation stack with a simple replacement (I pile one view on).

This works wonderfully. The storyboard and custom segue are just what the doctor ordered.

However, I want to give the user the choice of which of the 3 views they start with (becomes the root of the navigation stack). The storyboard insists on my selecting one of them as the root, so I do. However, at runtime, the user may want the initial view to be different, so I want the navigation stack to have a different view.

In a nib-based app, I'd simply instantiate a new controller from a nib, and replace the current root with that controller.

How do I pick out a controller and do that in a storyboard?

1
  • OK. I think I have it. It's a little bit roundabout, but the UIStoryboard::instantiateViewControllerWithIdentifier method is what I need. I just need to get the proper storyboard in place. I'll post the code when I have it working to my satisfaction. Mar 23, 2012 at 22:07

1 Answer 1

0

UPDATE: Thanks to a comment in another thread, I was able to simplify this a bit.

OK. I figured it out.

Instead of replacing the root, I simply push the appropriate controller on. This is because changing the root requires re-instantiating the controller, and the fox just ain't worth the chase. This has zero effect on the UX, so we do it this way:

/**************************************************************//**
 \brief Selects the initial search screen, depending on the user's choice.
 *****************************************************************/
- (void)selectInitialSearchAndForce:(BOOL)force         ///< If YES, then the screen will be set to the default, even if we were already set to one.
{
    UITabBarController  *tabController = (UITabBarController *)self.window.rootViewController;

    [tabController setSelectedIndex:0]; // Set the tab bar to the search screens.

    if ( force )
        {
        UIStoryboard    *st = [tabController storyboard];

        [[searchNavController navigationController] popToRootViewControllerAnimated:NO];

        UIViewController    *newSearch = nil;

        switch ( [[BMLT_Prefs getBMLT_Prefs] searchTypePref] )
            {
            case _PREFER_MAP_SEARCH:
            if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPad)   // The map and simple searches are combined, on the iPad.
                {
                newSearch = [st instantiateViewControllerWithIdentifier:@"map-search"];
                }
            break;

            case _PREFER_ADVANCED_SEARCH:
            newSearch = [st instantiateViewControllerWithIdentifier:@"advanced-search"];
           break;
            }

        if ( newSearch )    // We push the controller, as opposed to changing the root, because it's easier, and doesn't violate any of our ground rules.
            {
            [[searchNavController navigationController] pushViewController:newSearch animated:NO];
            // In the case of the iPad, we use a standard navbar push, so we need to prime the simple view to properly populate the Advanced view back button.
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
                {
                UIViewController    *simpleViewController = [[[searchNavController navigationController] viewControllers] objectAtIndex:0];

                simpleViewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString([[simpleViewController navigationItem] title], nil)
                                                                                                         style:UIBarButtonItemStyleBordered
                                                                                                        target:nil
                                                                                                        action:nil];
                }
            }
        }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.