0
votes

I have a UIViewController called "Yellow" in my storyboard that I'm adding like this:

  _detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Yellow"];
  _detailVC.location=cell.location;
  

  self.incomingView = _detailVC.view;
  [self addChildViewController:_detailVC];
  [self.view addSubview:self.incomingView];
  self.incomingView.alpha = 0;
  [_detailVC didMoveToParentViewController:self];

I need this UIViewController to exist within a UINavigationController as I'm using a push segue and it is giving me the following error:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'pushDetailVC'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'

If I go to the view for the "Yellow" view controller, and "embed in Navigation Controller", I still get the same problem.

enter image description here

How would I embed this UIViewController in a UINavigationController so that the push segue works correctly?

2
You should not have to add the VC(Yellow), and have to add the NC. The reason is when you add VC, NC is not in hierarchy of VCs and for performing push segues NC is needed. - Yogesh Suthar
so, I can't do what I'm trying to do? - timpone
Yes. For push segues to work NC is needed. - Yogesh Suthar
I get that and have it in a NC as can be seen in the image from IB; the NC isn't being recognized for some reason. - timpone
You didn't get what I am trying to say. You need to open NC not VC. - Yogesh Suthar

2 Answers

1
votes

When you instantiate a view controller from the storyboard, you don't automatically get any controller that it might be embedded in. You're telling the system to give you a DetailVC, and that's what it gives you. If you want the navigation controller, then you need to instantiate it, and it will automatically instantiate its rootViewController (which is Yellow) because its hooked up with a relationship segue.

UINavigationController *nav = [self.storyboard instantiateViewControllerWithIdentifier:@"Nav"];
_detailVC = (DetailVC *)nav.topViewController; // replace DetailVC with whatever your class name is
_detailVC.location=cell.location;
0
votes

To use a push segue, the UIViewController you are pushing from must be within the same UINavigationController as the one you are pushing to. If you don't want that top bar to be displayed on the view you are pushing from, then you should consider a different type of segue to make the Yellow view appear (though the yellow may still be within a Navigation Controller if it has its own hierarchy to take care of).