Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 940
Marcos Tanaka Avatar asked Jun 02 '26 17:06

Marcos Tanaka


2 Answers

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.

like image 192
Marcos Tanaka Avatar answered Jun 06 '26 07:06

Marcos Tanaka


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.

like image 44
Paulw11 Avatar answered Jun 06 '26 05:06

Paulw11



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!