Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissing presented view controller won't work

Tags:

ios

swift

I've got a kind of complicated modal segue setup in my project. I'm trying to dismiss a view controller another view controller previously presented. I'm doing so with this code:

if(self.presentedViewController != nil){
    print(self.presentedViewController!)
    self.presentedViewController!.dismiss(animated: false)
    print(self.presentedViewController!)
}

The prints are there for debugging purposes. They show that the presentedViewController doesn't actually get closed. Even though I've set animated to false, I still see an animation occuring in the app when dismiss is called. Yet, the VC doesn't actually get dismissed. Anyone knows a solution?

like image 483
Maximilian Krause Avatar asked Dec 02 '17 20:12

Maximilian Krause


People also ask

How do I remove a presented view controller?

To dismiss a modally presented view controller, call the view controller's dismiss(animated:completion:) method.

What happens when you dismiss a view controller?

The block to execute after the view controller is dismissed. This block has no return value and takes no parameters.


1 Answers

Apple

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.

dismiss(animated:completion:) dismisses the view controller that was presented modally by the view controller.

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621505-dismiss

If you present a view controller from the app's root, for example:

Presenting view controller

let root = UIApplication.shared.keyWindow!.rootViewController!
root.present(someViewController, animated: true, completion: nil)

You would dismiss it from the presented view controller like so:

Presented view controller

let root = UIApplication.shared.keyWindow?.rootViewController
root?.dismiss(animated: true, completion: nil)
like image 86
kidcoder SAVE UKRAINE Avatar answered Oct 14 '22 20:10

kidcoder SAVE UKRAINE