Marcos Tanaka
Marcos Tanaka

Reputation: 3336

Swift: navigationController returning nil

On a UIViewController instantiated by a UITabBarController, when I run the following code,

let destination = self.storyboard?.instantiateViewControllerWithIdentifier("test")
self.navigationController!.pushViewController(destination!, animated: true)

navigationController return nil. How can I get navigationController instance to push a view controller?

Upvotes: 3

Views: 14248

Answers (3)

user3810914
user3810914

Reputation: 425

https://stackoverflow.com/a/32959896/3810914

Exactly. If you did, the error would be resolved

enter image description here

enter image description here

Upvotes: 0

Marcos Tanaka
Marcos Tanaka

Reputation: 3336

As @Paulw11 said, what was missing was embed the UIViewController in a navigation controller. To do that, drag-and-drop a Navigation Controller to the Storyboard and CTRL + drag from that Navigation Controller to the UIViewController, chosing the relationship root view controller.

Upvotes: 7

Paulw11
Paulw11

Reputation: 114875

Your source UIViewController instance needs to be embedded in a UINavigationController in the storyboard scene in order to provide a navigation controller instance to push to.

Upvotes: 1

Related Questions