AlertController is not in the window hierarchy

前端 未结 8 1738
北荒
北荒 2020-11-29 03:01

I\'ve just created a Single View Application project with ViewController class. I would like to show a UIAlertController from a function which is located inside my own class

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 03:10

    Here is the code of an UIAlertController in a Utility.swift class (not a UIViewController) in Swift3, Thanks Mitsuaki!

    private func presentViewController(alert: UIAlertController, animated flag: Bool, completion: (() -> Void)?) -> Void {
        UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: flag, completion: completion)
    }
    func warningAlert(title: String, message: String ){
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:  { (action) -> Void in
        }))        
     //   self.present(alert, animated: true, completion: nil)
        presentViewController(alert: alert, animated: true, completion: nil)
    }
    

提交回复
热议问题