Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pushing a navigation controller is not supported

In my MainStoryBoard I want to push a viewController to the detailView but I get this error:

NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'

I set the identifier 'JSA' ID for the viewController on the storyboard.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     if (indexPath.row == 0) {         SWSJSAViewController *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"JSA"];         [self.navigationController pushViewController:viewController animated:YES];     } } 
like image 665
Jose Avatar asked Jul 19 '13 23:07

Jose


People also ask

How do I push navigation controller?

To push UINavigationController , first create a subclass of UIViewController , which will be a wrapper-/container- class for your UINavigationController and its content. This is a much more comprehensive answer which addresses the need to support hierarchical navigation controllers without modal presentation.

How do I use navigation controller in Swift?

Enter Swift as Language and choose Next. Go to the Storyboard and delete the View Controller. Open the Object Library and drag a Navigation Controller to the Storyboard. Select the Navigation Controller and go to The Attribute inspector.

How do I get navigation controller Viewcontroller?

The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property. To access the root view controller, we ask for the first item of the array of view controllers.


2 Answers

Like rmaddy said in the comments you are trying to push a navigation controller.

Navigation controllers should be presented (via presentViewController or they can be added as a childViewController) and ViewControllers should be pushed.

like image 147
Tiago Almeida Avatar answered Sep 24 '22 02:09

Tiago Almeida


When you talk about pushing Navigation Controller, it is most likely that you want to present it.

  1. Presenting UINavigationController

This is the most common way and this is what you want to do in most cases. UINavigationController cannot be pushed, it can only be presented with a new root View Controller.

MyViewController* vc = [[MyViewController alloc]       initWithNibName:@"MyController" bundle:nil];  UINavigationController *myNav = [[UINavigationController alloc] initWithRootViewController: vc];  [self presentViewController:myNav animated:YES completion:nil]; 

What you do here, is firstly create a UINavigationController and then set necessary UIViewController as its root controller.


  1. Pushing UINavigationController

If you have a hierarchy of ViewControllers and you need to push view controller that contains navigation controller within, steps are:

1) Push ViewController, containing UINavigationController.

To push UINavigationController, first create a subclass of UIViewController, which will be a wrapper-/container- class for your UINavigationController and its content.

ContainerViewController* vc = [[ContainerViewController alloc] init]; 

2) Adding UINavigationController as a child view controller

In viewDidLoad of your container (that you have just instantiated) simply add something like this:

Objective-C

UINavigationController* myNav = [[UINavigationController alloc] initWithRootViewController: rootViewController]; [myNav willMoveToParentViewController:self];  myNav.view.frame = self.view.frame;  //Set a frame or constraints [self.view addSubview:myNav.view]; [self addChildViewController: myNav]; [myNav didMoveToParentViewController:self]; 

Swift 4.2+

let childNavigation = UINavigationController(rootViewController: viewController) childNavigation.willMove(toParent: self) addChild(childNavigation) childNavigation.view.frame = view.frame view.addSubview(childNavigation.view) childNavigation.didMove(toParent: self) 

What you do here is basically instantiate your navigation controller and add it as a child controller to your wrapper. That's it. You have successfully pushed your UINavigationController.

like image 20
Hexfire Avatar answered Sep 26 '22 02:09

Hexfire