Wills Wills - 1 year ago 125
Swift Question

self.navigationController always returns nil using storyboard to storyboard navigation

Problem : I am trying to get the handle to navigation controller in my viewcontroller but it always returns nil, below is my code

Hierarchy : AppDelegate -> MainStoryboard -> nav Drawer -> TestingStoryboard -> testing

From Navigation Drawer i open the viewControl as below :

self.openNewStoryBoard(storyBoard: "testingone", scene: "initial")
func openNewStoryBoard(storyBoard:String, scene:String){
let storyboard = UIStoryboard(name: storyBoard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: scene) as UIViewController
self.present(controller, animated: true, completion: nil)


I have a navigationcontroller in my "testingone" storyboard which connects to a viewContorller "initial" via a relationship - "root view controller " to "viewController", connected this to testtingone class

In the code(testingOne) class i am trying to get the handle to navigationcontroller as below :

self.rootNav = self.navigationController!

But it always ends up nil

Answer Source

By directly presenting your view controller, you're bypassing the navigation controller all together. Thats why you're getting nil when calling self.navigationController. You should present your navigation controller instead.

To do that, in your story board simply set the Storyboard ID of the navigation controller to initial instead of the view controller.