How to programmatically dismiss UIAlertController without any buttons? How to programmatically dismiss UIAlertController without any buttons? ios ios

How to programmatically dismiss UIAlertController without any buttons?


Generally the parent view controller is responsible for dismissing the modally presented view controller (your popup). In Objective-C you would do something like this in the parent view controller:

[self dismissViewControllerAnimated:YES completion:nil];

The same code in Swift versions < 3 would be:

self.dismissViewControllerAnimated(true, completion: nil)

Swift 3.0:

self.dismiss(animated: true, completion: nil)


for swift you can do:

nameOfYourAlertController.dismiss(animated: true, completion: nil)

true will animate the disappearance, and false will abruptly remove the alert


If you want to post an alert that displays briefly, then dismisses itself, you could use the following method:

  func postAlert(title: String, message: String) {    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)    self.present(alert, animated: true, completion: nil)    // delays execution of code to dismiss    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: {      alert.dismiss(animated: true, completion: nil)    })  }