David Vincent Gagne David Vincent Gagne - 2 years ago 114
Swift Question

instantiateViewControllerWithIdentifier breaks UINavigationController flow

I have a UIViewController (with a hidden UINavigationController) which contains a UIButton. Clicking the button calls instantiateViewControllerWithIdentifier on another UIViewController (and passes the second UIViewController some data).

Try as I might, I could not get the second UIViewController to display its UINavigationController navigationBar, even though I had it embedded in the UINavigationController. That's not a catastrophe, though, because I didn't really want it there anyway and would have simply hidden it. I added a UIButton to the second UIViewController and control-click-dragged in Interface Builder to add a "Show" segue back to the first UIViewController.

But now when you visit any other UIViewController (and only after following the steps above), the UINavigationBar is missing.

Here's how I'm loading the second UIViewController:

let destinationViewController = self.storyboard?.instantiateViewControllerWithIdentifier("viewGold") as! SecondViewController
destinationViewController.results = result
self.presentViewController(destinationViewController, animated: true, completion: nil)


  1. Is it terrible of me to want to not display the UINavigationBar on two of the UIViewControllers?

  2. Am I loading the SecondViewController correctly?

  3. Is there a way to mimic clicking a "Back" UINavigationBar in the UIButton click of the SecondViewController?

Answer Source

So self.presentViewController is a modal presentation, which means that it is not included in your UINavigationController's navigation stack and thus will not display your navigation bar. You are presenting the right way here.

The problem lies in the segue you made from the second view controller to the first. The Show segue actually creates a new view controller and presents it modally, which is definitely not what you want (especially since it's creating your first view controller without it's navigation controller). Instead, use self.presentingViewController.dismissViewControllerAnimated to dismiss modally.

Presenting modally doesn't offer the same kind of transition that moving along the navigation stack does, which means you won't get your left/right transitions. Generally, I would recommend keeping your navigation bar visible and working on your navigation stack if you want to navigate like that.

For reference, look at Apple's documentation on segues!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download