3

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?

3
  • Is your UIViewController embedded in a navigation controller in the storyboard?
    – Paulw11
    Commented Oct 5, 2015 at 21:05
  • No, I forgot to embed it in a navigation controller. After doing that, the code worked. Thanks! Commented Oct 5, 2015 at 21:15
  • You should either answer your own question and accept that answer or delete the question.
    – Duncan C
    Commented Oct 5, 2015 at 21:53

3 Answers 3

7

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.

0
1

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.

2
  • This would not help if he instantiates the view controller directly. He would also have to instantiate the nav controller.
    – Charles A.
    Commented Oct 5, 2015 at 23:01
  • The issue isn't referring to the destination view controller - the view controller that was instantiated. It is saying that the current view controller (the one that he is trying to push the instantiated view controller onto) doesn't have a navigation controller
    – Paulw11
    Commented Oct 5, 2015 at 23:21
0

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

Exactly. If you did, the error would be resolved

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.