What is the best way to check if a UIAlertController is already presenting? What is the best way to check if a UIAlertController is already presenting? ios ios

What is the best way to check if a UIAlertController is already presenting?


It is not the UIAlertController that is "already presenting", it is MessagesMasterVC. A view controller can only present one other view controller at a time. Hence the error message.

In other words, if you have told a view controller to presentViewController:..., you cannot do that again until the presented view controller has been dismissed.

You can ask the MessagesMasterVC whether it is already presenting a view controller by examining its presentedViewController. If not nil, do not tell it to presentViewController:... - it is already presenting a view controller.


if ([self.navigationController.visibleViewController isKindOfClass:[UIAlertController class]]) {      // UIAlertController is presenting.Here}


Well, the suggested solutions above has an essential problem from my point of view:

If you ask your ViewController, whether the attribute 'presentedViewController' is nil and the answer is false, you can't come to the conclusion, that your UIAlertController is already presented. It could be any presented ViewController, e.g. a popOver. So my suggestion to surely check, whether the Alert is already on the screen is the following (cast the presentedViewController as a UIAlertController):

if self.presentedViewController == nil {   // do your presentation of the UIAlertController   // ...} 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 thePresentedVCAsAlertController : UIAlertController = thePresentedVC as? UIAlertController {         // nothing to do , AlertController already active         // ...         print("Alert not necessary, already on the screen !")      } else {         // there is another ViewController presented         // but it is not an UIAlertController, so do          // your UIAlertController-Presentation with          // this (presented) ViewController         // ...         thePresentedVC!.presentViewController(...)         print("Alert comes up via another presented VC, e.g. a PopOver")      }  }

}