0

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

public static void
  • 1,153
  • 11
  • 20
  • Hi, for navigation to the another nib file either you are presenting modally that nib controller or pushing it on to the same navigation controller. In any of this case if you will just pop out(in case of push) or dismiss the nib controller(in case of modal), you will get back to the presented controller. If this is not what you need then please give more specification – if-else-switch Jun 19 '14 at 11:29
  • Why are not you pushing your instantiated VC to navigation stack, instead of showing it modal! you should have used `[viewController.navigationController pushViewController:vc animated:YES]` instead – Ayan Sengupta Jun 19 '14 at 11:41
  • I don't think this has the desire effect – public static void Jun 19 '14 at 11:53

2 Answers2

0

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.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Hi, thanks for the help. So, I don't want to get rid of my navigation bar; it's more that I don't want to lose it when I relaunch the storyboard. How do I start/dismiss new view controllers such that it functions as you've said? – public static void Jun 19 '14 at 11:38
  • either present it as a modal, then dismiss it when you're done, or push it onto your navigation stack (hiding the nav bar if desired) and then pop it off the stack when you're done. In either case the navigation stack remains under the new VC that comes from a nib. – Duncan C Jun 19 '14 at 13:50
0

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.

public static void
  • 1,153
  • 11
  • 20
  • You're right, this isn't the ideal solution, as it's dependent on the UIViewController being of a given Kind. Duncan's solution is the correct approach. Frankly, you simply need to maintain a count of viewControllers added to the Navigation Stack and popViewControllerAnimated: the appropriate number of times. Far simpler than trying to maintain your own navigation stack. – MDB983 Jun 19 '14 at 14:01