如何弹出视图控制器,然后显示UIAlertController

时间:2018-07-26 19:28:17

标签: swift swift4 uialertcontroller

说ViewController2中有错误,我想回到以前的视图控制器ViewController1,然后显示警报。现在,如果我将其放入ViewController2

    @IBAction func testing(_ sender: Any) {
    navigationController?.popViewController(animated: true)
    Alert.alert(userTitle: "Error", userMessage: " ", userOptions: " ", in: LandingViewController() as UIViewController)

}

将其用作Alert类

    public class Alert {
    class func alert(userTitle: String?, userMessage: String, userOptions: String, in vc: UIViewController) {
        DispatchQueue.main.async {
            let alert = UIAlertController(title: userTitle, message: userMessage, preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: userOptions, style: UIAlertActionStyle.default, handler: nil))
            vc.present(alert, animated: true, completion: nil)
        }

    }

}

它将引发错误

那么有办法吗?

1 个答案:

答案 0 :(得分:3)

问题是LandingViewController() as UIViewController不在UiWindow中呈现

尝试

guard let nav = navigationController, let top = nav.topViewController else {
    return
}

nav.popViewController(animated: true)
Alert.alert(userTitle: "Error", userMessage: " ", userOptions: " ", in: top)