I am presenting two view controller like so
self.presentViewController(choosePlace, animated: true, completion: nil)
self.presentViewController(shareController, animated: true, completion: nil)
and I want to dismiss both of them like this:
println("parent \(self.parentViewController)")
self.dismissViewControllerAnimated(true, completion: {
self.parentViewController?.dismissViewControllerAnimated(true, completion: nil)
})
self.parentViewController is always nil though. How can I dismiss two at the same time?
You can:
self.dismissViewControllerAnimated(true, completion: {
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
})
So, make sure that you are presenting your view controllers in order:
parentViewController -> choosePlace -> shareController
(arrows indicate "self.presentViewController")
Swift 4
There where some issue I faced while trying Michael Voline's answer.
As the comment on the answer it did not work for me. it was because of the presentingViewController
was nil.
So we need to set in a different property say presentingController
but this will not work if you are setting it on the viewDidLoad
private var presentingController: UIViewController?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
presentingController = presentingViewController
}
then
dismiss(animated: false, completion: {
self.presentingController?.dismiss(animated: false)
})
I made the animation false so that the user will not see the UIof presenting view controller in the split seconds of dismissing.
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