Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss current view controller and parentviewcontroller?

Tags:

ios

swift

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?

like image 525
Tyler Avatar asked Jun 28 '15 19:06

Tyler


2 Answers

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")

like image 191
Michael Voline Avatar answered Oct 23 '22 09:10

Michael Voline


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.

like image 21
shinoy Avatar answered Oct 23 '22 09:10

shinoy