说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)
}
}
}
它将引发错误
那么有办法吗?
答案 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)