Push UINavigationController programmatically. iOS9

2019-05-31 06:38发布

问题:

I want to display a navigation controller programmatically. This code works perfect on iOS 8, but on iOS 9 it doesn't work.

var vc = storyboard?.instantiateViewControllerWithIdentifier("view_controller_id") as! UIViewController;
var nc = UINavigationController()
        nc.setViewControllers([vc], animated: false)
        navigationController?.pushViewController(nc, animated: true)

After that I am getting this error

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

回答1:

You can't push a navigation controller to another navigation controller. You will have to show your navigation view controller with viewcontrollers modally or just push your view controllers to the active navigation controller.