Ios Swift:从实用程序类调用UIAlertController

Ios Swift:从实用程序类调用UIAlertController,ios,swift,function,class,uialertcontroller,Ios,Swift,Function,Class,Uialertcontroller,作为一名快速开发人员,我目前正在努力学习和提高我的技能,这可能会被认为是一个愚蠢的问题,但我很好奇 问题 在我的代码中,我不断重复UIAlertController的创建和表示代码,以至于看起来很马虎。同样,通过将其分派到主线程,它最多需要5行代码,我在整个项目中多次在多个视图控制器上重复此代码。因此,我创建了一个实用程序类,在该类中,我有一个显示UIAlertController的函数 问题: 我想知道的是,这是一种糟糕的编码实践吗?不断地从另一个类调用这个函数,不断地创建一个新的UIAler

作为一名快速开发人员,我目前正在努力学习和提高我的技能,这可能会被认为是一个愚蠢的问题,但我很好奇

问题 在我的代码中,我不断重复UIAlertController的创建和表示代码,以至于看起来很马虎。同样,通过将其分派到主线程,它最多需要5行代码,我在整个项目中多次在多个视图控制器上重复此代码。因此,我创建了一个实用程序类,在该类中,我有一个显示UIAlertController的函数

问题: 我想知道的是,这是一种糟糕的编码实践吗?不断地从另一个类调用这个函数,不断地创建一个新的UIAlertController,这是草率的吗?这会减慢我的申请完成吗?或者这很好

代码(如有必要):

class Utils {

func displayError(viewController: UIViewController, title: String, message: String, action: UIAlertAction?) {
    let ac = UIAlertController(title: title,
                               message: message,
                               preferredStyle: .alert)

    if action == nil {
        ac.addAction(UIAlertAction(title: "Ok", style: .cancel))
    } else {
        ac.addAction(action!)
    }

    DispatchQueue.main.async {
        viewController.present(ac, animated: true)
    }
}
}

提前谢谢你

您可以使用窗口的rootviewcontroller属性来显示警报,而不是将视图控制器作为参数传递。以下是一个例子:

class Utils {

    static func displayAlert(title: String, message: String) {

        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "Default", style: .default, handler: nil)
        alertController.addAction(defaultAction)

        guard let viewController = UIApplication.shared.keyWindow?.rootViewController else {
            fatalError("keyWindow has no rootViewController")
            return
        }

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

}
不要忘记将函数定义为静态函数,以便能够以这种方式调用它:

Utils.displayAlert(title: "Hello", message: "This is an alert")
对于swift 5,iOS 13* 包括解析:iOS 13.0中不推荐使用“keyWindow” 我更喜欢将我的消息保存在一个单独的类GlobMessage:UIAlertController{中,从各种VC调用它们。 基于Youssef和


我想把它放在UIViewController扩展而不是utils类中。实际上,这似乎是一个非常好的主意,谢谢。
class GlobMessage: UIAlertController {
    static func MessageXYZ(){
        ///needs 'extension UIWindow'
        ///'static' allows to call from various VC‘s

        
        if let keyWindow = UIWindow.key {
            //found calling VC 

            //create text for Message
            let header:String = //"⚠️ deactivated!"

            let body01:String = """

                your message here.
                Second row of message.
                """

            // assemble body 
            let body:String = body01 //+ body02 + body03 + body04 + body05 + body06 + body07 + body08 + body09 + body10 + body11 + body12 + body13 + body14 + body15 + body16 + body17 + body18 + body19 + body20

            //buttons with functions
            let OK = UIAlertAction(title: "OK", style: .default) {
               UIAlertAction in
               //your code here
           }
            let NOK = UIAlertAction(title: "❌not jet", style: .destructive) {
                UIAlertAction in
                //your code here
            }

            //assemble message
            let Message = UIAlertController(title: header, message: body, preferredStyle: .alert)
            Message.addAction(OK)
            //Message.addAction(NOK)
            
            //present message
            keyWindow.rootViewController!.present(Message, animated: true, completion: nil)
        }//end if let keyWindow
    }//end static func MessageXYZ()

}//end class GlobMessage


//MARK: -
extension UIWindow {
    /// source: https://stackoverflow.com/questions/57134259/how-to-resolve-keywindow-was-deprecated-in-ios-13-0
    /// what’s the calling VC?:
    /// Usage:
    /// if let keyWindow = UIWindow.key {
    ///    // Do something
    ///    ....
    ///    keyWindow.rootViewController!.present(Message, animated: true, completion: nil)
    /// }//end if let keyWindow
    ///
    static var key: UIWindow? {
        if #available(iOS 13, *) {
            return UIApplication.shared.windows.first { $0.isKeyWindow }
        } else {
            return UIApplication.shared.keyWindow
        }//end if else
    }//end static var key
}//end extension UIWindow