Swift:从实用程序调用 UIAlertController Class

Swift: Calling UIAlertController from a Utilities Class

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

问题
在我的代码中,我不断重复 UIAlertController 创建和演示代码,以至于看起来很草率。同样将它分派到主线程,它最多需要 5 行,并且我在整个项目中多次在多个视图控制器上重复此代码。因此,我创建了一个 "Utilities" class 并且在那个 class 中我有一个显示 UIAlertController 的函数。

问题
我想知道的是,这是糟糕的编码习惯吗?不断地从另一个 class 调用这个函数,不断地创建一个新的 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)
    }
}
}

在此先感谢您。

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

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

对于swift5,iOS13*

包括。解决:'keyWindow' 在 iOS 13.0

中被弃用

我更喜欢将我的消息保存在单独的 class class GlobMessage: UIAlertController { 中,从不同的 VC 调用它们。 基于 Youssef 和

的回答
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: 
    /// 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 { [=10=].isKeyWindow }
        } else {
            return UIApplication.shared.keyWindow
        }//end if else
    }//end static var key
}//end extension UIWindow