尝试呈现....其视图不在 window 层次结构中

Attempt to present....whose view is not in the window hierarchy

我一直在努力让它发挥作用。我已经编写了一个函数来尝试检查用户是否已登录,如果他们没有登录,他们将看到一个登录屏幕,但我不断收到一个错误,提示“尝试呈现......其视图不在”中window 层次结构!它显示默认的故事板,下面是代码

func checkuser () {
    let currentUser = FIRAuth.auth()?.currentUser
    if currentUser != nil {
        self.view.window?.rootViewController = storyboard?.instantiateViewController(withIdentifier: "eventsstoryboard")
        print("user logged in")
    }   
    else
    {

        let vc = storyboard?.instantiateViewController(withIdentifier: "loginvc") as! loginviewcontroller
        self.present(vc,animated: true, completion: nil)
        print("user not logged in")

    }
}
checkuser()

检查你的 if 部分, 您是否将 viewController 呈现为:

let vc = storyboard?.instantiateViewController(withIdentifier: "eventsstoryboard") as! someviewcontroller
self.present(vc,animated: true, completion: nil)
print("user logged in")

感谢大家使用以下方法让它工作。使用 viewdidappear 并解决了它

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)

    func checkuser () {
        let currentUser = FIRAuth.auth()?.currentUser
        if currentUser != nil
        {
            self.view.window?.rootViewController = storyboard?.instantiateViewController(withIdentifier: "eventsstoryboard")
            print("user logged in")
        }

        else
        {

            let vc = storyboard?.instantiateViewController(withIdentifier: "loginvc") as! loginviewcontroller
            self.present(vc,animated: true, completion: nil)
            print("user not logged in")

        }
    }
    checkuser()

}