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")
}
I could see the println message in console output but ViewController never gets dismissed. What could be the problem?
When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it.
Select the button that should make the UIViewController Disappear and drag it to the UIViewController you want to go to. In my case it shows **dismiss Controller* because of the name of my Class. Select it and you are done!
How To Present / Dismiss UIViewController’s View In Swift Example. 1 There is a yellow background button with blue text in the main UIViewController view. 2 When you click the button, it will present the second UIViewController view. 3 The second UIView window has red background color, and there is a Dismiss This View button in it. More items...
For reference, be aware that you might be dismissing the wrong view controller. For example, if you have an alert box or modal showing on top of another modal. (You could have a Twitter post alert showing on top of your current modal alert, for example). In this case, you need to call dismiss twice, or use an unwind segue.
The second UIView window has a red background color, and there is a Dismiss This View button in it. When you click the Dismiss This View button, it will dismiss the second UIView and return back to the main UIView.
When you develop an iOS app use swift in Xcode, the project template commonly creates a default UIViewController class defined in ViewController.swift file. This UIViewController class is used as a controller of the MVC pattern to respond to the app’s main window view’s event.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With