How to dismiss ViewController in Swift? How to dismiss ViewController in Swift? swift swift

How to dismiss ViewController in Swift?


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)


I have a solution for your problem. Please try this code to dismiss the view controller if you present the view using modal:

Swift 3:

self.dismiss(animated: true, completion: nil)

OR

If you present the view using "push" segue

self.navigationController?.popViewController(animated: true)


if you do this i guess you might not get println message in console,

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