如何弹出视图控制器然后显示 UIAlertController
How to pop view controller and then show UIAlertController
说ViewController2有错误,我想回到ViewController1,之前的view controller,然后显示一个alert。现在如果我把它放在 ViewController2
@IBAction func testing(_ sender: Any) {
navigationController?.popViewController(animated: true)
Alert.alert(userTitle: "Error", userMessage: " ", userOptions: " ", in: LandingViewController() as UIViewController)
}
将其用作警报class
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)
}
}
}
会报错
那么有办法做到这一点吗?
问题是 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)
说ViewController2有错误,我想回到ViewController1,之前的view controller,然后显示一个alert。现在如果我把它放在 ViewController2
@IBAction func testing(_ sender: Any) {
navigationController?.popViewController(animated: true)
Alert.alert(userTitle: "Error", userMessage: " ", userOptions: " ", in: LandingViewController() as UIViewController)
}
将其用作警报class
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)
}
}
}
会报错
那么有办法做到这一点吗?
问题是 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)