What is the best way to check if a UIAlertControll

2019-01-16 15:30发布

I have a tableview which, when loaded, each cell could possibly return an NSError, which I have chosen to display in a UIAlertController. Problem is I get this error in the console if multiple errors are returned.

Warning: Attempt to present UIAlertController: 0x14e64cb00 on MessagesMasterVC: 0x14e53d800 which is already presenting (null)

Ideally, I would ideally like to handle this in my UIAlertController extension method.

class func simpleAlertWithMessage(message: String!) -> UIAlertController {

    let alertController = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    let cancel = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)

    alertController.addAction(cancel)
    return alertController
}

Based on matt's answer, I changed the extension to a UIViewController extension, its much cleaner and saves lots of presentViewController code.

    func showSimpleAlertWithMessage(message: String!) {

    let alertController = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    let cancel = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)

    alertController.addAction(cancel)

    if self.presentedViewController == nil {
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

9条回答
虎瘦雄心在
2楼-- · 2019-01-16 15:49

This category can auto-manage all the modal controllers include of UIAlertController.

UIViewController+JCPresentQueue.h

查看更多
祖国的老花朵
3楼-- · 2019-01-16 15:54

Here's a solution I use in Swift 3. It is a function that shows an alert to the user, and if you call it multiple times before the user has dismissed the alert, it will add the new alert text to the alert that's already being presented. If some other view is being presented, the alert will not appear. Not all will agree with that behavior, but it works well for simple situations.

extension UIViewController {
    func showAlert(_ msg: String, title: String = "") {
        if let currentAlert = self.presentedViewController as? UIAlertController {
            currentAlert.message = (currentAlert.message ?? "") + "\n\nUpdate:\(title): \(msg)"
            return
        }

        // create the alert
        let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))

        // show the alert
        self.present(alert, animated: true, completion: nil)
    }
}
查看更多
小情绪 Triste *
4楼-- · 2019-01-16 15:57

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")
      }
  }

}

查看更多
看我几分像从前
5楼-- · 2019-01-16 16:01
if ([self.navigationController.visibleViewController isKindOfClass:[UIAlertController class]]) {

      // UIAlertController is presenting.Here

}
查看更多
Luminary・发光体
6楼-- · 2019-01-16 16:02

We can simply check if any view is presented.

if presented then check if it is kind of UIAlertController .

    id alert = self.presentedViewController;

    if (alert && [alert isKindOfClass:[UIAlertController class]]) 
      {
           *// YES UIAlertController is already presented*
      }
    else
       {
        // UIAlertController is not presented OR visible.
       }
查看更多
闹够了就滚
7楼-- · 2019-01-16 16:04

Simply dismiss the current controller and present the one you want i.e.

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

self.displayAlertController()

查看更多
登录 后发表回答