返回上一个 ViewController 无法处理警报错误消息

Pop back to previous ViewController not working on Alert Error Message

当用户没有正确输入他们的所有详细信息时,我会弹出一个警告。当用户关闭警报时,我希望它弹回到之前的 ViewController。

看起来像这样

func errorAlert (errorMessage: String?) {
    let alert = UIAlertController (title: "Error message", message: errorMessage, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
    self.present(alert, animated: true, completion: nil)
    popThisView()
    SVProgressHUD.dismiss()
}

func popThisView() {
    self.dismiss(animated: false, completion: nil)
    self.navigationController!.popToRootViewController(animated: true)
}

但是 XCode 给我这个错误:

popToViewController:transition: called on while an existing transition or presentation is occurring; the navigation stack will not be updated.

这样做,

let alert = UIAlertController (title: "Error message", message: errorMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler:{ (alertOKAction) in
                self.popThisView()
            }))
self.present(alert, animated: true, completion: nil)
SVProgressHUD.dismiss()

UIAlertAction 对象有一个动作处理程序,因此您可以在该处理程序中处理动作:

let action =  UIAlertAction(title: "OK", style: . cancel, handler: {(alert: UIAlertAction!) in
            DispatchQueue.main.async( execute: {
                self.popThisView()
            })
        })