0
votes

I have a UIStoryboard that consists of 4 to 5 ViewControllers. These ViewControllers are connected with each other through push segue. This story board is not embedded inside NavigationController.

Now i have to use this UIStoryboard on two different scenario in my application.

  1. This UIStoryboard is pushed inside a ViewController which is already inside a NavigationController so i can simply do like this.

    UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:@"myStoryboard" bundle:nil]; UIViewController *vc = (UIViewController *)[myStoryboard instantiateInitialViewController]; [self.navigationController pushViewController:vc animated:YES];

This works perfectly fine as we are pushing the UIStoryboard inside a NavigationController so for further navigation UIStoryboard's ViewControllers can use the NavigationController in which the story board is pushed.

  • The second scenario is that is have to modally present this UIStoryBoard from a viewController which i can do like.

    UIStoryboard *myStoryBoard = [UIStoryboard storyboardWithName:@"myStoryBoard" bundle:nil]; UIViewController *vc = [myStoryBoard instantiateInitialViewController];

    [self presentViewController:vc animated:YES completion:^{

            }];
    

It is presented successfully but now as the UIStoryboard is not in any NavigationController when i do a push Seque from first ViewController to second ViewController inside the UIStoryboard it crashes (which it should) saying

Could not find a navigation controller for segue.  Push segues can only be used when the source controller is managed by an instance of UINavigationController.'

Can some body tell how i can solve this problem or how i can programmatically embed or remove the UIStoryboard inside the NavigationController

1

1 Answers

0
votes

It will not work because you are triggering the root segue from a controller which is push modally and not contained in a navigation controller.

You probably want to swap your hierarchy around so that the main view is in the nav controller which is the window root view controller and the other view controller is push modally.

You actually have to set your UIViewController as the root controller of a UINavigationController (or pushed through from a view controller wia navcontroller). To do that

  1. Drag a new navigation controller into your storyboard before view controller

  2. Right click on the navgiation controller, and connect the "Root View Controller" property to your existing view controller.

  3. Move the entry point arrow from your view controller to the root view controller

Hope this will help.