How to create uialertcontroller in global swift

后端 未结 12 1733
情深已故
情深已故 2020-12-05 12:09

I\'m trying to create uialertcontroller in Config.swift file as follow.

static func showAlertMessage(titleStr:String, messageStr:St         


        
12条回答
  •  星月不相逢
    2020-12-05 12:48

    self.window would mean that there's a window object in this class, and it's not the case.

    You would need to use your let window : UIWindow? with window?.presentViewController(alert, animated: true, completion: nil), but this won't help, since this window does not actually represent any existing window, and it's not a view controller anyway.

    So I suggest you pass the actual view controller you'll be using to the method:

    static func showAlertMessage(vc: UIViewController, titleStr:String, messageStr:String) -> Void {
        let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.Alert);
        vc.presentViewController(alert, animated: true, completion: nil)
    }
    

    and you call it from a class where a UIViewController object is available.

提交回复
热议问题