Ios 在所有视图控制器中创建警报功能-swift

Ios 在所有视图控制器中创建警报功能-swift,ios,swift,uiviewcontroller,uialertview,uialertcontroller,Ios,Swift,Uiviewcontroller,Uialertview,Uialertcontroller,我正在尝试声明一个用于在我的应用程序中显示警报的函数。为了避免重复工作,我尝试对我的所有应用程序使用相同的功能。我试图通过创建一个带有showNotification函数的类来实现这一点。但是,当我创建该类的对象并调用该方法时,什么也没有发生。我该怎么做 class SharedPropertiesAndMetods : UIViewController { func showNotification(title: String, message: String) {

我正在尝试声明一个用于在我的应用程序中显示警报的函数。为了避免重复工作,我尝试对我的所有应用程序使用相同的功能。我试图通过创建一个带有showNotification函数的类来实现这一点。但是,当我创建该类的对象并调用该方法时,什么也没有发生。我该怎么做

class SharedPropertiesAndMetods : UIViewController {

    func showNotification(title: String, message: String)
    {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "تائید", style: .default, handler: nil)
        alertController.addAction(defaultAction)
        present(alertController, animated: true, completion: nil)
    }

}

实际上,您可以在类之外的任何地方声明一个简单的方法

func showAlertWithCompletion(message:String,okTitle:String,cancelTitle:String?,completionBlock:@escaping (_ okPressed:Bool)->()){
    let alertController = UIAlertController(title: AppName, message: message, preferredStyle: .alert)
    let okAction = UIAlertAction(title: okTitle, style: .default) { (ok) in
        completionBlock(true)
    }
    alertController.addAction(okAction)
    if let cancelTitle = cancelTitle{
        let cancelOption = UIAlertAction(title: cancelTitle, style: .cancel, handler: { (axn) in
            completionBlock(false)

        })
        alertController.addAction(cancelOption)
    }

    if let topController = UIWindow.topViewController(){
      topController.present(alertController, animated: true, completion: nil)
    }

}

这样,无论您在哪里调用它,您都会在完成句柄中按下ok按钮callback,甚至按照@Ganesh Kumar所述进行扩展,使用这样的扩展

extension UIViewController {
  func showAlert(title: String, message: String) {
    let alertController = UIAlertController(title: title, message:
      message, preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {action in
    }))
    self.present(alertController, animated: true, completion: nil)
  }
}
self.showAlert(title: "hi", message: "test")
像这样调用函数

extension UIViewController {
  func showAlert(title: String, message: String) {
    let alertController = UIAlertController(title: title, message:
      message, preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {action in
    }))
    self.present(alertController, animated: true, completion: nil)
  }
}
self.showAlert(title: "hi", message: "test")

为什么不只是一个扩展

extension UIViewController {

    func showNotification(title: String, message: String)
    {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "تائید", style: .default, handler: nil)
        alertController.addAction(defaultAction)
        present(alertController, animated: true, completion: nil)
    }
}

我要做的是创建一个“通用”视图控制器来执行该作业,然后从中继承:

1。如果要在每次视图出现时显示警报:

class GenericViewController: UIViewController {

    // MARK: - View lifecycle -

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if let notification = self.shouldDisplayAlertNotification() {
            self.showNotification(notification)
        }
    }

    // MARK: - Internal methods -

    func shouldDisplayAlertNotification() -> AlertNotification? {
        return nil
    }

    // MARK: - Private methods -

    private func showNotification(_ alertNotification: AlertNotification) {
    }

}

class MyController: GenericViewController {

    override func shouldDisplayAlertNotification() -> AlertNotification? {
        return AlertNotification(title: "Title", message: "Message")
    }

}
其中AlertNotification是您的自定义模型类:

class AlertNotification {
    var title: String
    var message: String

    init(title: String, message: String) {
        self.title = title
        self.message = message
    }
}
这样,只有覆盖
shouldDisplayAlertNotification
的VC才会显示警报

2。如果要显示“按需”警报:

class GenericViewController: UIViewController {

    // MARK: - View lifecycle -

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if let notification = self.shouldDisplayAlertNotification() {
            self.showNotification(notification)
        }
    }

    // MARK: - Internal methods -

    func shouldDisplayAlertNotification() -> AlertNotification? {
        return nil
    }

    // MARK: - Private methods -

    private func showNotification(_ alertNotification: AlertNotification) {
    }

}

class MyController: GenericViewController {

    override func shouldDisplayAlertNotification() -> AlertNotification? {
        return AlertNotification(title: "Title", message: "Message")
    }

}
根据建议,扩展UIViewController

extension UIViewController {
    func showNotification(title: String, message: String) {
    }
}

你也可以在你的应用程序中创建一个util文件,你可以添加任何可重用的方法或函数,并在你的应用程序中的任何地方使用它,比如

进口基础 导入UIKit

//马克:-警惕

func showMessage(标题:String,消息:String!,VC:UIViewController){


}

您可以创建alertController的扩展,还可以选择操作处理程序。这将允许根据是否需要处理程序使用两个不同的警报控制器

extension  UIAlertControler {

class func genericErrorAlert(forAlert message: String, completion: ((UIAlertAction) -> Void)? = nil )

    let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: completion))

    return alert
  }
}

您可以使用此视图控制器扩展在整个应用程序中显示警报视图。


将此方法添加为的扩展UIViewController@HosAp有4个答案。“没有一个是有用的?”GaneshKumar是的,作为扩展添加是一个好方法idea@GaneshKumar是的,对不起,我没注意到。如果你同意,卢卡的回答更全面。感谢您的帮助。这样,当我想在VC中使用ViewDidDisplay时,它会覆盖GenericViewContoller的ViewDidDisplay。是吗?是的,但您可以调用super.viewdid作为第一条指令出现,所以将显示警报