Reputation: 3085
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?
Upvotes: 245
Views: 412778
Reputation: 1829
So if you wanna dismiss your Viewcontroller use this. This code is written in button action to dismiss VC
@IBAction func cancel(sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
Upvotes: 6
Reputation: 71
Since you used push presented viewController, therefore, you can use
self.dismiss(animated: false, completion: nil)
Upvotes: 6
Reputation: 472
If you using the present method in the parent VC then you should call this function, to dismiss the child VC use this
self.dismiss(animated: true, completion: nil)
if you calling child VC by using push method, to dismiss the child VC use this
self.navigationController?.popViewController(animated: true)
Upvotes: 5
Reputation: 559
@IBAction func back(_ sender: Any) {
self.dismiss(animated: false, completion: nil)
}
Upvotes: 2
Reputation:
Try this:
@IBAction func close() {
dismiss(animated: true, completion: nil)
}
Upvotes: 3
Reputation: 5920
Use:
self.dismiss(animated: true, completion: nil)
instead of:
self.navigationController.dismissViewControllerAnimated(true, completion: nil)
Upvotes: 15
Reputation: 5866
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)
Upvotes: 461
Reputation: 4803
From Apple documentations:
The presenting view controller is responsible for dismissing the view controller it presented
Thus, it is a bad practise to just invoke the dismiss method from it self.
What you should do if you're presenting it modal is:
presentingViewController?.dismiss(animated: true, completion: nil)
Upvotes: 12
Reputation: 17882
In Swift 4.1 and Xcode 9.4.1
If you use pushViewController to present new view controller, use this
self.navigationController?.popViewController(animated: false)
Upvotes: 2
Reputation: 38634
Based on my experience, I add a method to dismiss me as extension to UIViewController:
extension UIViewController {
func dismissMe(animated: Bool, completion: (()->())?) {
var count = 0
if let c = self.navigationController?.viewControllers.count {
count = c
}
if count > 1 {
self.navigationController?.popViewController(animated: animated)
if let handler = completion {
handler()
}
} else {
dismiss(animated: animated, completion: completion)
}
}
}
Then I call this method to dismiss view controller in any UIViewController
subclass. For example, in cancel action:
class MyViewController: UIViewController {
...
@IBAction func cancel(sender: AnyObject) {
dismissMe(animated: true, completion: nil)
}
...
}
Upvotes: 7
Reputation: 413
In Swift 3.0 to 4.0 it's as easy as typing this into your function:
self.dismiss(animated: true, completion: nil)
Or if you're in a navigation controller you can "pop" it:
self.navigationController?.popViewController(animated: true)
Upvotes: 20
Reputation: 752
In Swift 3.0
If you want to dismiss a presented view controller
self.dismiss(animated: true, completion: nil)
Upvotes: 2
Reputation: 2834
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)
Upvotes: 197
Reputation: 2406
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")
}
}
Upvotes: 22
Reputation: 51
Don't create any segue from Cancel or Done to other VC and only write this code your buttons @IBAction
@IBAction func cancel(sender: AnyObject) {
dismiss(animated: false, completion: nil)
}
Upvotes: 5
Reputation: 893
If you are presenting a ViewController modally, and want to go back to the root ViewController, take care to dismiss this modally presented ViewController before you go back to the root ViewController otherwise this ViewController will not be removed from Memory and cause Memory leaks.
Upvotes: 2
Reputation: 2305
If you presenting a controller without a Navigation Controller, you can call the following code from a method of the presented controller.
self.presentingViewController?.dismiss(animated: true, completion: nil)
If your ViewController is presented modally, optional presentingViewController will be not nil and the code will be executed.
Upvotes: 11
Reputation: 628
add this line into the brackets:
self.dismissViewControllerAnimated(true, completion: nil)
Upvotes: 15
Reputation: 1154
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.
Upvotes: 3
Reputation: 10344
Here is the one way to dismiss present view controller and move back to previous view controller. You can do this through Storyboard only.
Please try this, It's working with me.
Second Way - Use - navigationController.popViewControllerAnimated(true)
Best luck..
Upvotes: 4