如何在没有动作的情况下呈现新的UIViewController?

时间:2015-05-10 14:42:48

标签: ios swift

如果用户已登录,我尝试推送新的UIViewController,但我并不真正了解它失败的位置。

    if (FBSDKAccessToken.currentAccessToken() != nil)
    {
        returnUserData()
        var storyboard: UIStoryboard = UIStoryboard(name:"Main", bundle:nil)

        var vc: UINavigationController = storyboard.instantiateViewControllerWithIdentifier("loggedInViewController") as! UINavigationController

        self.presentViewController(vc, animated: true, completion: nil)
        // User is already logged in, do work such as go to next view controller.
    }

以下是错误消息:

User Logged In 2015-05-10 16:35:18.010 noscrubs[32134:43488091] Warning: Attempt to present <UINavigationController: 0x7f86dc282920> on <noscrubs.ViewController: 0x7f86dc279aa0> whose view is not in the window hierarchy!

1 个答案:

答案 0 :(得分:2)

您过早地展示视图控制器。在尝试呈现任何内容之前,您应该等待在视图控制器上调用viewDidAppear:方法。

在显示视图之前调用viewDidLoad,这是通过将视图添加到窗口层次结构来完成的。加载视图后,将添加视图并调用viewDidAppear。