Attempt to present UIAlertController on View Controller which is already presenting (null) [Swift]

The problem is really simple, you are trying to display another UIAlertController on the currently presented UIAlertController.

So, how to solve such a case?

  1. You need to get a list of all UIAlertController's you use in your current view controller.

  2. You have to check the logic for displaying alerts in your current view controller (or other view controllers if you are doing async requests).

  3. Your code must be like this when you want to display one alert on top of another.

Assume loadingAlert is currently displaying on the screen:

self.loadingAlert.dismiss(animated: true, completion: {
     let anotherAlert = UIAlertController(title: "New One", message: "The Previous one is dismissed", preferredStyle: .alert)
     let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
     anotherAlert.addAction(okAction)
     self.present(anotherAlert, animated: true, completion: nil)
})

You have to dismiss the first one before the next one can appear. I made this answer for dismissing an alert without buttons on it to make it more efficient.

So, what about the alert with action buttons?

It will dismiss automatically when you click one of the action buttons on UIAlertController that you created.

But, if you are displaying two UIAlertControllers which include UIButtons at the same time, the problem will still occur. You need to re-check the logic for each, or you can handle it in the handler for each action :

self.connectionErrorAlert.dismiss(animated: true, completion: {
     let anotherAlert = UIAlertController(title: "New One", message: "The Previous one is dismissed", preferredStyle: .alert)
     let okAction = UIAlertAction(title: "OK", style: .default, handler: {action in
            let nextAlert = UIAlertController(title: "New One", message: "The Previous one is dismissed", preferredStyle: .alert)
            self.present(nextAlert, animated: true, completion: nil)
     })
     anotherAlert.addAction(okAction)
     self.present(anotherAlert, animated: true, completion: nil)
})

For an Answer to Mike :

DispatchQueue.main.async(execute: {

      if self.presentedViewController == nil {
           print("Alert comes up with the intended ViewController")
           var inputTextField = UITextField()

           let textPrompt = UIAlertController(title: "Test", message: "Testing", preferredStyle: .alert)

           textPrompt.addAction(UIAlertAction(title: "Continue", style: .default, handler: {
               (action) -> Void in
               // if the input matches the required text

               let str = inputTextField.text
               if str == requireTextInput {
                    print("right")
               } else {
                    print("wrong")
               }

           }))

           textPrompt.addTextField(configurationHandler: {(textField: UITextField!) in
                textField.placeholder = ""
                inputTextField = textField

            })
            weakSelf?.present(textPrompt, animated: true, completion: nil)
      } else {
            // either the Alert is already presented, or any other view controller
            // is active (e.g. a PopOver)
            // ...
            let thePresentedVC : UIViewController? = self.presentedViewController as UIViewController?
            if thePresentedVC != nil {
                 if let _ : UIAlertController = thePresentedVC as? UIAlertController {
                      print("Alert not necessary, already on the screen !")

              } else {

                      print("Alert comes up via another presented VC, e.g. a PopOver")
              }
            }
       }
})

Thanks to @Luke Answer : https://stackoverflow.com/a/30741496/3378606


Hy, I tried a more simple solution that seems to work, and is able to present a second alert in front of the first, that will stay (no need to dismiss before user answers the question) :

if self.presentedViewController==nil{
    self.present(MyAlert, animated: true, completion: nil)
}else{
    self.presentedViewController!.present(MyAlert, animated: true, completion: nil)
}