【问题标题】:Warning: Attempt to present <UIAlertController:> on <App name:> whose view is not in the window hierarchy警告:尝试在视图不在窗口层次结构中的 <App name:> 上显示 <UIAlertController:>
【发布时间】: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)
        }
    }
}

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

【问题讨论】:

    标签: ios swift xcode cocoa-touch alert


    【解决方案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]'
    • 对不起,想法是改变根视图控制器,我用正确的代码编辑我的答案
    猜你喜欢
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    相关资源
    最近更新 更多