简体   繁体   中英

How do I instantiate a storyboard with a given root viewcontroller and initial viewcontroller?

I have a storyboard in my application with a navigation controller and several views. This automatically puts a navigation bar with a back button into any views that are not the root view.

However, sometimes I navigate away from this storyboard to an individual nib. I want to navigate back to the storyboard, but not necessarily to the original root view. I currently use this method to do so:

+(void) TransitionOnStoryboard:(NSString*)storyboard to:(NSString*)identifier withViewController:(UIViewController*)viewController
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboard bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:identifier];
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [viewController presentViewController:vc animated:YES completion:NULL];
}

This shows the view I want but without the navigation bar. How do I specify my navigation controller or root view, such that the app knows to put a navigation bar with a back button in?

Thanks

The answer is to leave your navigation controller underneath the view controller you add from a nib.

Present the nib as a full0-screen modal. That gets rid if your navigation bar, as desired. From that new view controller, you can push more modals, add a navigation controller, or whatever.

Note that you could do all of this and stay inside your storyboard as well.

Once you are done, dismiss the modal to reveal your navigation controller, and you are back in business with your storyboard. You can push a new view controller onto your navigation controller without animation and it should appear as the front-most VC when you pop the modal that came from a nib.

I'm sure that this isn't the ideal way to solve this problem, but it did work very nicely for me.

Essentially, I removed all the views from the view controller that had been generated since I navigated away from the storyboard, but before the current view and popped the current view. In this case, these views were of one class (CheckboxListViewController) and so could be removed quite simply as below:

+(void) navigateToMainMenu:(UINavigationController*)navigationController
{
    [QuickView removeFromNavigationController:navigationController allOfViewControllerWithClass:[CheckboxListViewController class]];
    [navigationController popViewControllerAnimated:YES];
}

+(void) removeFromNavigationController:(UINavigationController *)navigationController allOfViewControllerWithClass:(Class)viewControllerClass
{
    NSMutableArray *keptViewControllers = [[NSMutableArray alloc]init];
    for (UIViewController *viewController in navigationController.viewControllers)
        if (![viewController isKindOfClass:viewControllerClass])
            [keptViewControllers addObject:viewController];
    navigationController.viewControllers = keptViewControllers;
}

(note- QuickView is the name of the class that contains these methods.).

Any other classes that you do not want your pop to navigate back to can be removed by calling:

[QuickView removeFromNavigationController:navigationController allOfViewControllerWithClass:[YourClassName class]];

In the navigateToMenu method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM