Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy

后端 未结 30 3107
一整个雨季
一整个雨季 2020-11-21 23:39

Just started using Xcode 4.5 and I got this error in the console:

Warning: Attempt to present < finishViewController: 0x1e56e0a0 > on < ViewCont

30条回答
  •  花落未央
    2020-11-21 23:55

    Sadly, the accepted solution did not work for my case. I was trying to navigate to a new View Controller right after unwind from another View Controller.

    I found a solution by using a flag to indicate which unwind segue was called.

    @IBAction func unwindFromAuthenticationWithSegue(segue: UIStoryboardSegue) {
        self.shouldSegueToMainTabBar = true
    }
    
    @IBAction func unwindFromForgetPasswordWithSegue(segue: UIStoryboardSegue) {
        self.shouldSegueToLogin = true
    }
    

    Then present the wanted VC with present(_ viewControllerToPresent: UIViewController)

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        if self.shouldSegueToMainTabBar {
            let mainTabBarController = storyboard.instantiateViewController(withIdentifier: "mainTabBarVC") as! MainTabBarController
            self.present(mainTabBarController, animated: true)
            self.shouldSegueToMainTabBar = false
        }
        if self.shouldSegueToLogin {
            let loginController = storyboard.instantiateViewController(withIdentifier: "loginVC") as! LogInViewController
            self.present(loginController, animated: true)
            self.shouldSegueToLogin = false
        }
    }
    

    Basically, the above code will let me catch the unwind from login/SignUp VC and navigate to the dashboard, or catch the unwind action from forget password VC and navigate to the login page.

提交回复
热议问题