如何创建可重用的 UIAlertcontroller?

How to create reusable UIAlertcontroller?

我想为我的 project.I 创建一个可重复使用的 UIAlertController class 尝试了以下 code.But 我的警报不是 showing.What 我的代码或任何代码中的错误help.This 是我的警报 class

class AlertView: NSObject {

 class func showAlert(view: UIViewController , message: String){

        let alert = UIAlertController(title: "Warning", message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        view.presentViewController(alert, animated: true, completion: nil)
    }
}

我在我的另一个视图控制器中调用它,比如

class ViewController: UIViewController {

  override func viewDidLoad() {
      super.viewDidLoad()
      AlertView.showAlert(self, message: "Test alert")
      // Do any additional setup after loading the view, typically from a nib.
   }
}

main_queue

中显示您的警报
class AlertView: NSObject {

    class func showAlert(view: UIViewController , message: String) {
        let alert = UIAlertController(title: "Warning", message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

        dispatch_async(dispatch_get_main_queue(), {
            view.presentViewController(alert, animated: true, completion: nil)
        })
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        AlertView.showAlert(self, message: "Test alert")
        // Do any additional setup after loading the view, typically from a nib.
    }
}

您可以使用 Anbu Karthiks 的回答。作为替代方案,我使用 Swift 2s 协议扩展来显示警报。

import UIKit

protocol Alertable { }
extension Alertable where Self: UIViewController {

func showAlert(title title: String, message: String) {

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

    let okAction = UIAlertAction(title: "OK", style: .Cancel) { _ in }
    alertController.addAction(okAction)

    DispatchQueue.main.async {       
       self.view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
    }
}

 func showAlertWithSettings(title title: String, message: String) {

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

    let okAction = UIAlertAction(title: "OK", style: .Cancel) { _ in }
    alertController.addAction(okAction)

    let settingsAction = UIAlertAction(title: "Settings", style: .Default) { _ in
       guard let url = NSURL(string: UIApplicationOpenSettingsURLString) else { return }
        UIApplication.sharedApplication().openURL(url)
    }
    alertController.addAction(settingsAction)

    DispatchQueue.main.async {       
         self.view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
   }
}

现在,在您需要显示警报的 viewController 中,您只需遵守协议

class ViewController: UIViewController, Alertable { } 

并调用类似

的方法
showAlert(title: "Alert title", message: "Alert message")

就好像它们是 viewController 本身的一部分。

注意:正如成员在评论中所说,viewDidLoad 可能很快就会显示警报。尝试使用轻微的延迟或 ViewDidAppear

希望对您有所帮助

swift3&4

中的可重用警报
class AlertView: NSObject {
    class func showAlert(view: UIViewController , title: String , message: String){
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
        view.present(alert, animated: true, completion: nil)
    }
}

 //How to use?
 //Alert.showAlert(view: self, title: "Alert!", message: "Reusable Alert")