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

后端 未结 13 2057
后悔当初
后悔当初 2020-12-04 17:26

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

相关标签:
13条回答
  • 2020-12-04 17:39

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-04 17:39

    We can simply check if any view controller 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.
           }
    
    0 讨论(0)
  • 2020-12-04 17:39

    Dismiss the current controller and present the alert controller like

     func alert(_ message:String) {
      let alert = UIAlertController(title: "Error!", message: message, preferredStyle: .alert)
      alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
      self.dismiss(animated: false, completion: nil)
      self.present(alert, animated: true,completion: nil)
        }
    
    0 讨论(0)
  • 2020-12-04 17:43

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

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

    self.displayAlertController()

    0 讨论(0)
  • 2020-12-04 17:46

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

    }

    0 讨论(0)
  • 2020-12-04 17:47

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

    UIViewController+JCPresentQueue.h

    0 讨论(0)
提交回复
热议问题