Saketram Durbha Saketram Durbha - 2 months ago 28
Swift Question

Segue Out of Navigation Controller

I am trying to leave the initial view controller, and go into the blank view controller. That is fine, but that would make the blank view controller also part of the navigation controller, which is not what I want. I want to segue out of the view controller.

In the view controller I try to segue out of, it pops it self, and when I try the method in the view will appear of the target view controller,

self.navigationController?.topViewController
returns itself, but
self.navigationController?.popViewControllerAnimated(animated)
doesn't work

enter image description here

Answer

If you have a navigationController do

self.navigationController?.popViewControllerAnimated(false)

Otherwise do

self.dismissViewControllerAnimated(false, completion: nil)

Update

Go to your Storyboard, select the ViewController you want to navigate to and add a storyboard id. Make sure the click "Use Storyboard ID" enter image description here

Go to the class you want to navigate from and add the following code

let storyboard = UIStoryboard(name: "Main", bundle: nil)
// vc is the Storyboard ID that you added
// as! ... Add your ViewController class name that you want to navigate to
let controller = storyboard.instantiateViewControllerWithIdentifier("vc") as! ViewController
self.presentViewController(controller, animated: true, completion: { () -> Void in
})

Add this code in the action that you use when you want to navigate.