How to dismiss ViewController in Swift?

rshankar picture rshankar · Jul 10, 2014 · Viewed 325k times · Source

I am trying to dismiss a ViewController in swift by calling dismissViewController in an IBAction

  @IBAction func cancel(sender: AnyObject) {
    self.dismissViewControllerAnimated(false, completion: nil)
    println("cancel")
}

@IBAction func done(sender: AnyObject) {
    self.dismissViewControllerAnimated(false, completion: nil)
    println("done")
}

random image of a segue

I could see the println message in console output but ViewController never gets dismissed. What could be the problem?

Answer

Zoon Nooz picture Zoon Nooz · Jul 10, 2014

From you image it seems like you presented the ViewController using push

The dismissViewControllerAnimated is used to close ViewControllers that presented using modal

Swift 2

navigationController.popViewControllerAnimated(true)

Swift 4

navigationController?.popViewController(animated: true)

dismiss(animated: true, completion: nil)