0
votes

I am trying to open a UINavigationController programmatically. Here is my code:

UIViewController *vc = [[UIStoryboard storyboardWithName:@"Storyboard"
  bundle:nil] instantiateViewControllerWithIdentifier:@"ViewControllerID"];
UINavigationController *nc = [[UINavigationController alloc] 
   initWithRootViewController:vc];
nc.modalTransitionStyle = UIModalPresentationNone;
[self presentViewController:nc animated:YES completion:NULL];

I keep getting the following Error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'

4
I've pushed navigation controllers exactly like this plenty of times, except that I've created the rootViewController (vc in your code) with initWithNib: bundle: I suggest temporarily changing the creation of the viewController (maybe use a plain UIViewController rather than your own class) to that to see if it works. I am guessing maybe your problem is how this viewController is wired into the storyboard. - Jef
I'm guessing your vc is actually a UINavigationController and not a UIViewController. You also shouldn't be setting modalTransitionStyle to UIModalPresentationNone, that's not even the right type of enum. - dan

4 Answers

3
votes

you are completely right, but instead presenting, just push your navigation controller.

For Ex.

UIViewController *vc = [[UIStoryboard storyboardWithName:@"Storyboard"
  bundle:nil] instantiateViewControllerWithIdentifier:@"ViewControllerID"];
UINavigationController *nc = [[UINavigationController alloc] 
initWithRootViewController:vc];
[self pushViewController:nc animated:YES];
0
votes

You should do this:

UIViewController *viewControllrer = [[UIStoryboard storyboardWithName:@"Storyboard" bound :nil] instantiateViewControllerWithIdentifier:@"ViewControllerID" ]; UINavigationController *rootViewController = [[UINavigationController alloc] initWithRootViewController:viewController]; self.navigationController pushViewController:rootViewController animated:YES];

-1
votes

The message is pretty specific. It's telling you that you can't present a navigation controller modally.

-1
votes

Create the navigation controller inside the view controller. Add as subview the referenced nav controller's view in another presented view controller.