Adding a view to the window hierarchy

后端 未结 1 1741
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 18:03

I am trying to create a pause screen on my game. I have added a \'PauseScreen\' viewController in my storyboard with the Storyboard ID and Restoration ID set as \"PauseScree

1条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-02 18:41

    The error is telling you exactly what's going on.

    Warning: Attempt to present on whose view is not in the window hierarchy!

    (UIApplication.sharedApplication().delegate as AppDelegate).window?.rootViewController is pointing to an instance of StartScreenViewController. This is bad: rootViewController should point to an instance of GameScene.

    The root cause must be how GameScene is presented. From your description:

    The "StartScreenViewController" is the view controller… It then goes to the "GameScene"…

    This must be where your problem is. How do you go to GameScene from StartScreenViewController?

    My guess is that you are adding a new window to the application. You need to set the rootViewController instead.

    let gameScene = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("GameSceneID") as UIViewController
    let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    appDelegate.window?.rootViewController = gameScene
    

    When you go back to the start screen, you again set the rootViewController.

    let initialViewController = UIStoryboard(name: "Main", bundle:nil).instantiateInitialViewController() as UIViewController
    let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    appDelegate.window?.rootViewController = initialViewController
    

    You can use transitionFromViewController(,toViewController:, duration:, options:, animations:, completion:) to animate setting the root view controller.

    0 讨论(0)
提交回复
热议问题