Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does UIAlertController's dismiss method without animation execute synchronously?

If I call (In an UIViewController)

alert.dismiss(animated: false, completion: nil)
print("executed after dismiss?")

Given that alert is a previously presented UIAlertController, does the dismiss method execute synchronously?

Could I do:

alert.dismiss(animated: false, completion: nil)
let newAlert = UIAlertController(...)
present(newAlert, animated: true, completion: nil)

Without having to worry about a problem while presenting newAlert?

like image 327
gsobrevilla Avatar asked Feb 05 '23 05:02

gsobrevilla


1 Answers

Like most animation methods when you dismiss the UIAlertController you are just getting the ball rolling. The view controller is dismissed and removed asynchronously. To test this we can use some code like this:

let alert = UIAlertController(title: "Oh No!", message: ":(", preferredStyle: UIAlertControllerStyle.alert)

self.present(alert, animated: true, completion: nil)
print(presentedViewController) // Optional(<UIAlertController: 0x7fefe901e670>)

alert.dismiss(animated: true, completion: nil)
print(presentedViewController) // Optional(<UIAlertController: 0x7fefe901e670>) 

As demonstrated above the alert view controller is not removed as the presented view controller right away. You can still present the new alert right after dismissing the old alert. However best practice would be to place your code for the second alert in the completion handler of the first. This completion handler is called after viewDidDisappear is called on the presented view controller.

like image 79
Alex Pelletier Avatar answered Feb 07 '23 18:02

Alex Pelletier