警告:尝试在视图不在窗口层次结构中的 <App name:> 上显示 <UIAlertController:>

Posted

技术标签:

【中文标题】警告:尝试在视图不在窗口层次结构中的 <App name:> 上显示 <UIAlertController:>【英文标题】:Warning: Attempt to present <UIAlertController:> on <App name:> whose view is not in the window hierarchy 【发布时间】:2019-12-03 07:55:29 【问题描述】:

我收到此错误是因为我在另一个 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)
        
    

在我的情况下如何正确显示警报....

【问题讨论】:

【参考方案1】:

尝试在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,因此您应该在view 层次结构中获取最新呈现的viewController,或者尝试在rootViewController.

您可以查看this 以获取当前呈现的viewController

【讨论】:

亲爱的兄弟,我在这里为整个项目提供相同的警报例如。如果 TextFiled 为空,请输入 Textfield like。没事吧…… 因为这里我们在root VC中展示【参考方案2】:

随便用

self.navigationController?.popToRootViewController(animated: false)

 self.navigationController?.setViewControllers([LoginViewController], animated: true)

而不是

self.navigationController?.viewControllers.removeAll()

【讨论】:

其实这里 loginVC 不是我的 root VC ,dashboard VC in root VC。所以我想让Login VC作为root vc。然后,如果我编写此代码self.navigationController?.popToRootViewController(animated: false),它将起作用。我说的对吗…… 是的,或者你可以做一些类似 self.navigationController?.viewControllers = [LoginViewController] 这个self.navigationController是什么意思?.viewControllers = [LogoinViewControllre] 错误:Cannot assign value of type '[LoginViewController].Type' to type '[UIViewController]' 对不起,想法是改变根视图控制器,我用正确的代码编辑我的答案

以上是关于警告:尝试在视图不在窗口层次结构中的 <App name:> 上显示 <UIAlertController:>的主要内容,如果未能解决你的问题,请参考以下文章

警告:尝试呈现不在窗口层次结构中的视图!在 UISearchController

警告:尝试在视图不在窗口层次结构中的 **.ViewController 上呈现 ZMNavigationController

Xamarin.Forms 警告:尝试使用 iOS 图像/手势识别器在其视图不在窗口层次结构中的 * 上呈现 *

警告:尝试显示不在窗口层次结构中的视图!迅速

警告“尝试呈现视图不在窗口层次结构中的 ViewController - Objective C

警告:尝试在其视图不在窗口层次结构中的 ViewController 上呈现 ViewController (w/UIAlertController)