尝试显示....其视图不在窗口层次结构中

时间:2018-01-23 07:38:15

标签: ios swift3

我一直在努力让这个工作起来。我已经编写了一个函数来尝试检查用户是否已登录,如果不是,他们将会看到登录屏幕,但我一直收到一个错误,即尝试显示....其视图不在窗口层次结构!它显示默认的故事板,下面是代码

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()

2 个答案:

答案 0 :(得分:0)

检查你的零件, 你将viewController呈现为:

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

答案 1 :(得分:0)

感谢所有人使用以下方法工作。使用了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()

}