警告:尝试在视图不在 window 层次结构中的 <App name:> 上呈现 <UIAlertController:>
Warning: Attempt to present <UIAlertController:> on <App name:> whose view is not in the window hierarchy
我收到这个错误是因为我在另一个 VC 中显示警报,但如何解决这个问题。
我的代码是:
{
let message = res[0]["message"] as! String
//If session expaired move to login page
if message == "Session Expired" {
//Session Expired
DispatchQueue.main.async {
//Remove all navigations
self.navigationController?.viewControllers.removeAll()
//Check whether the user logined or not
UserDefaults.standard.set(false, forKey: "isUserLoggedIn")
//Clear user defaults
SharedClass.sharedInstance.clearDataFromUserDefaults()
let lvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LVC") as! LoginViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = lvc
}
}
//Call alert function
self.showAlert(title: "", msg: message)
}
警报功能:
//Alert function
extension UIViewController {
func showAlert(title: String, msg: String) {
DispatchQueue.main.async {
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
如何在我的案例中正确显示警报....
尝试在 rootViewController
上将 alertController
呈现为,
extension UIViewController {
func showAlert(title: String, msg: String) {
DispatchQueue.main.async {
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
(UIApplication.shared.delegate as! AppDelegate).window?.rootViewController?.present(alert, animated: true, completion: nil)
}
}
}
您正在调用 showAlert
方法的 viewController
当前未在 view
层次结构中显示 viewController
,因此您应该获取最新显示的 viewController
在 view
层级中或尝试在 rootViewController
.
上展示
您可以检查 this 以获取当前显示的 viewController
。
只需使用
self.navigationController?.popToRootViewController(animated: false)
或
self.navigationController?.setViewControllers([LoginViewController], animated: true)
而不是
self.navigationController?.viewControllers.removeAll()
我收到这个错误是因为我在另一个 VC 中显示警报,但如何解决这个问题。
我的代码是:
{
let message = res[0]["message"] as! String
//If session expaired move to login page
if message == "Session Expired" {
//Session Expired
DispatchQueue.main.async {
//Remove all navigations
self.navigationController?.viewControllers.removeAll()
//Check whether the user logined or not
UserDefaults.standard.set(false, forKey: "isUserLoggedIn")
//Clear user defaults
SharedClass.sharedInstance.clearDataFromUserDefaults()
let lvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LVC") as! LoginViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = lvc
}
}
//Call alert function
self.showAlert(title: "", msg: message)
}
警报功能:
//Alert function
extension UIViewController {
func showAlert(title: String, msg: String) {
DispatchQueue.main.async {
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
如何在我的案例中正确显示警报....
尝试在 rootViewController
上将 alertController
呈现为,
extension UIViewController {
func showAlert(title: String, msg: String) {
DispatchQueue.main.async {
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
(UIApplication.shared.delegate as! AppDelegate).window?.rootViewController?.present(alert, animated: true, completion: nil)
}
}
}
您正在调用 showAlert
方法的 viewController
当前未在 view
层次结构中显示 viewController
,因此您应该获取最新显示的 viewController
在 view
层级中或尝试在 rootViewController
.
您可以检查 this 以获取当前显示的 viewController
。
只需使用
self.navigationController?.popToRootViewController(animated: false)
或
self.navigationController?.setViewControllers([LoginViewController], animated: true)
而不是
self.navigationController?.viewControllers.removeAll()