解散当前提供的所有UIAlertControllers

时间:2015-02-20 19:22:30

标签: ios swift uiwindow uialertcontroller

有没有办法解雇当前出现的所有UIAlertControllers?

这是因为从我的应用程序的任何地方和任何状态,我需要在按下推送通知时到达某个ViewController。

6 个答案:

答案 0 :(得分:4)

您可以将UIAlertController子类化,将NSNotification观察者附加到每个会触发UIAlertController子类中的方法以解除警报控制器,然后发布NSNotification每当你准备好解雇时,ex:

class ViewController: UIViewController {
    func presentAlert() {
        // Create alert using AlertController subclass
        let alert = AlertController(title: nil, message: "Message.", preferredStyle: UIAlertControllerStyle.Alert)
        // Add observer to the alert
        NSNotificationCenter.defaultCenter().addObserver(alert, selector: Selector("hideAlertController"), name: "DismissAllAlertsNotification", object: nil)
        // Present the alert
        self.presentViewController(alert, animated: true, completion:nil)
    }
}

// AlertController subclass with method to dismiss alert controller
class AlertController: UIAlertController {
    func hideAlertController() {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

然后在您准备好解除警报时发布通知(在这种情况下,按下推送通知时):

NSNotificationCenter.defaultCenter().postNotificationName("DismissAllAlertsNotification", object: nil)

答案 1 :(得分:2)

我在swift 4中编写了更多通用代码, 此类使用实用程序类显示警报。

import UIKit

let APP_ORANGE_COLOR = UIColor(red: 1.000, green: 0.412, blue: 0.000, alpha: 1.00)

extension UIAlertController {
    @objc func hideAlertController() {
        self.dismiss(animated: false, completion: nil)
    }
}

class AlertUtility: UIViewController {

    static func showAlert(title: String!, message : String!, viewController: UIViewController) {
        let alert = UIAlertController(title: title, message: message ,preferredStyle: UIAlertControllerStyle.alert)

        alert.addAction(UIAlertAction(title: NSLocalizedString("ok", comment: "ok"), style: UIAlertActionStyle.cancel, handler: nil))
        alert.view.tintColor = APP_ORANGE_COLOR

        NotificationCenter.default.addObserver(alert, selector: #selector(alert.hideAlertController), name: DismissAllAlertsNotification, object: nil)

        viewController.present(alert, animated: true, completion: nil)
    }

    static func showAlertAutoDismiss(title: String!, message : String!) -> Void {
        //let appDelegate = UIApplication.shared.delegate as! AppDelegate

        // the alert view
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

        let topWindow = UIWindow(frame: UIScreen.main.bounds)
        topWindow.rootViewController = UIViewController()
        topWindow.windowLevel = UIWindowLevelAlert + 0.8

        topWindow.makeKeyAndVisible()
        topWindow.rootViewController?.present(alert, animated: true, completion: {})

        // change to desired number of seconds (in this case 5 seconds)
        let when = DispatchTime.now() + 1
        DispatchQueue.main.asyncAfter(deadline: when){
            // your code with delay
            alert.dismiss(animated: true, completion: nil)
            topWindow.isHidden = true
        }
    }

    static func showAlert(title: String!, message : String!) -> Void {
        //let appDelegate = UIApplication.shared.delegate as! AppDelegate

        // the alert view
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

        let topWindow = UIWindow(frame: UIScreen.main.bounds)
        topWindow.rootViewController = UIViewController()
        topWindow.windowLevel = UIWindowLevelAlert + 1
        topWindow.makeKeyAndVisible()
        topWindow.rootViewController?.present(alert, animated: true, completion: {})

        alert.addAction(UIAlertAction(title: NSLocalizedString("ok", comment: "ok"), style: UIAlertActionStyle.cancel, handler: {(_ action: UIAlertAction) -> Void in
            // continue your work
            // important to hide the window after work completed.
            // this also keeps a reference to the window until the action is invoked.
            topWindow.isHidden = true
        }))

        NotificationCenter.default.addObserver(alert, selector: #selector(alert.hideAlertController), name: DismissAllAlertsNotification, object: nil)
        alert.view.tintColor = APP_ORANGE_COLOR
    }


    static func showGenericErrorMessageAlert(viewController: UIViewController) {
        let alert = UIAlertController(title: NSLocalizedString("error", comment: ""), message: NSLocalizedString("generic.error.message", comment: "") ,preferredStyle: UIAlertControllerStyle.alert)

        alert.addAction(UIAlertAction(title: NSLocalizedString("ok", comment: "ok"), style: UIAlertActionStyle.cancel, handler: nil))
        alert.view.tintColor = APP_ORANGE_COLOR
        viewController.present(alert, animated: true, completion: nil)
        NotificationCenter.default.addObserver(alert, selector: #selector(alert.hideAlertController), name: DismissAllAlertsNotification, object: nil)
    }

    static func showComingSoonAlert(viewController: UIViewController) {

        // the alert view
        let alert = UIAlertController(title: "", message: NSLocalizedString("coming.soon", comment: ""), preferredStyle: .alert)

        viewController.present(alert, animated: true, completion: {})

        // change to desired number of seconds (in this case 5 seconds)
        let when = DispatchTime.now() + 1
        DispatchQueue.main.asyncAfter(deadline: when){
            // your code with delay
            alert.dismiss(animated: true, completion: nil)
        }
        NotificationCenter.default.addObserver(alert, selector: #selector(alert.hideAlertController), name: DismissAllAlertsNotification, object: nil)
    }

    // Show alert view with call back
    static func showAlertWithCB(title: String, message: String, isConditional: Bool, viewController: UIViewController, completionBlock: @escaping (_: Bool) -> Void) {

        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.view.tintColor = APP_ORANGE_COLOR
        // Check whether it's conditional or not ('YES' 'NO, or just 'OK')
        if isConditional
        {
            alert.addAction(UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: UIAlertActionStyle.cancel, handler: { (action: UIAlertAction) in
                alert.dismiss(animated: true, completion: nil)
                completionBlock(false)
            }))

            alert.addAction(UIAlertAction(title: NSLocalizedString("yes", comment: ""), style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in
                alert.dismiss(animated: true, completion: nil)
                completionBlock(true)
            }))

        }
        else
        {
            alert.addAction(UIAlertAction(title: NSLocalizedString("ok", comment: "ok"), style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in
                alert.dismiss(animated: true, completion: nil)
                completionBlock(true)
            }))
        }

        NotificationCenter.default.addObserver(alert, selector: #selector(alert.hideAlertController), name: DismissAllAlertsNotification, object: nil)
        viewController.present(alert, animated: true, completion: nil)
    }

    static func showAlertWithTextField(viewController : UIViewController,completionBlock: @escaping (_: Bool, String) -> Void) {

        //1. Create the alert controller.
        let alert = UIAlertController(title: "Report Event?", message: "", preferredStyle: .alert)
        alert.view.tintColor = APP_ORANGE_COLOR
        //2. Add the text field. You can configure it however you need.
        //AlertUtility.addte
        alert.addTextField { (textField) in

            let heightConstraint = NSLayoutConstraint(item: textField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50)
            textField.addConstraint(heightConstraint)
            textField.placeholder = "Enter report reason here"
            textField.tintColor = APP_ORANGE_COLOR
            textField.autocapitalizationType = .sentences
        }

        // 3. Grab the value from the text field, and print it when the user clicks OK.
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { [weak alert] (_) in
            // Force unwrapping because we know it exists.
            completionBlock(true,"")
            //print("Text field: \(textField.text)")
        }))

        // 3. Grab the value from the text field, and print it when the user clicks OK.
        alert.addAction(UIAlertAction(title: "Submit", style: .default, handler: { [weak alert] (_) in
            let textField = alert?.textFields![0] // Force unwrapping because we know it exists.
            completionBlock(true,(textField?.text)!)
            //print("Text field: \(textField.text)")
        }))

        // 4. Present the alert.
        viewController.present(alert, animated: true, completion: nil)

        let textField = alert.textFields![0]
        let v = UIView.init(frame: textField.frame)
        textField.addSubview(v)
        v.frame = textField.frame
        v.bounds = textField.bounds
        v.backgroundColor = APP_ORANGE_COLOR
        v.superview?.bringSubview(toFront: v)
    }

}

以这种方式使用

//sample code - use in your view controller
AlertUtility.showAlertWithCB(title: NSLocalizedString("alert", comment: "") , message: (error)!, isConditional: false, viewController: self, completionBlock: { (yes) in

                      //Your actions on callback
                        self.popToPreviousController()
                    })
AlertUtility.showAlert(title: ALERT_TITLE, message: message, viewController: self)

当您需要/需要在应用中自动关闭提醒时发布通知

let DismissAllAlertsNotification = Notification.Name("DismissAllAlertsNotification")

NotificationCenter.default.post(name: DismissAllAlertsNotification, object: nil)

答案 2 :(得分:2)

func dismissAnyAlertControllerIfPresent() {
    guard let window :UIWindow = UIApplication.shared.keyWindow , var topVC = window.rootViewController?.presentedViewController else {return}
    while topVC.presentedViewController != nil  {
        topVC = topVC.presentedViewController!
    }
    if topVC.isKind(of: UIAlertController.self) {
        topVC.dismiss(animated: false, completion: nil)
    }
}

这对我有用!

答案 3 :(得分:1)

您可以解雇当前以这种方式呈现给用户的UIAlertController

self.dismissViewControllerAnimated(true, completion: nil)

答案 4 :(得分:1)

它们是模态的:任何时候都只会有其中一个,它会完全聚焦。

您有责任创建警报,用户应该解雇警报。如果需要进一步控制,则需要制作(或使用)自定义视图以显示一叠消息。

答案 5 :(得分:0)

我使用以下扩展程序实现了这一目标,希望对您有所帮助

首先;您将拥有UIApplication的扩展来检索RootViewController

extension UIApplication {

   static func topViewControllerInNavigationStack(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
       if let navigationController = controller as? UINavigationController {
            return topViewControllerInNavigationStack(controller: navigationController.visibleViewController)
       }
       if let tabController = controller as? UITabBarController {
           if let selected = tabController.selectedViewController {
             return topViewControllerInNavigationStack(controller: selected)
           }
       }
       if let presented = controller?.presentedViewController {
           return topViewControllerInNavigationStack(controller: presented)
       }
       return controller
   }
}

第二; UIAlertViewController的扩展

extension UIAlertController {
     static func dismissPresentedAlertViewController() {
         let viewController = UIApplication.topViewControllerInNavigationStack()
         guard let isKindOf = viewController?.isKind(of: 
         UIAlertController.classForCoder()), isKindOf else {
             return
         }
         viewController?.dismiss(animated: false, completion: nil)
    }
}