将视图添加到窗口层次结构

时间:2022-10-30 09:23:17

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 "PauseScreenID" and to move to the pause screen I have have created the function in "GameScene" :

我想在我的游戏上创建一个暂停屏幕。我在故事板中添加了一个'PauseScreen'viewController,其中Storyboard ID和Restoration ID设置为“PauseScreenID”,并移动到暂停屏幕我在“GameScene”中创建了该功能:

func pauseSceenTransition(){

    let viewController = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("PauseScreenID") as UIViewController

    let currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate)

    currentViewController.window?.rootViewController?.presentViewController(viewController, animated: false, completion: nil)
}

However when the it gets called, I get the error :

但是当它被调用时,我得到错误:

Warning: Attempt to present <AppName.PauseScreen: 0x7fae61fe5ff0> on <AppName.StartScreenViewController: 0x7fae61f79980> whose view is not in the window hierarchy!

警告:尝试在 上显示 ,其视图不在窗口层次结构中! :0x7fae61fe5ff0> :0x7fae61f79980>

The "StartScreenViewController" is the view controller for my start screen and is the initial view controller. It then goes to the "GameScene" which is where the "PauseScreen" needs to go. It works if I make the initial view controller the "GameViewController"/"GameScene" so I presume that I need to change the second line:

“StartScreenViewController”是我的开始屏幕的视图控制器,是初始视图控制器。然后转到“GameScene”,这是“PauseScreen”需要去的地方。如果我将初始视图控制器设置为“GameViewController”/“GameScene”,它可以工作,所以我认为我需要更改第二行:

let currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate)

so that it is presenting the "PauseScreen" on the "GameViewController", not on the "StartScreenViewController" but I'm not sure how to do that.

所以它在“GameViewController”上呈现“PauseScreen”,而不是“StartScreenViewController”,但我不知道该怎么做。

1 个解决方案

#1


13  

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

错误告诉您到底发生了什么。

Warning: Attempt to present <AppName.PauseScreen: 0x7fae61fe5ff0> on <AppName.StartScreenViewController: 0x7fae61f79980> whose view is not in the window hierarchy!

警告:尝试在 上显示 ,其视图不在窗口层次结构中! :0x7fae61fe5ff0> :0x7fae61f79980>

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

(UIApplication.sharedApplication()。delegate as AppDelegate).window?.rootViewController指向StartScreenViewController的一个实例。这很糟糕:rootViewController应该指向GameScene的一个实例。

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

根本原因必须是GameScene的呈现方式。从您的描述:

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

“StartScreenViewController”是视图控制器......然后转到“GameScene”......

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

这一定是您的问题所在。你如何从StartScreenViewController转到GameScene?

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

我的猜测是你要为应用程序添加一个新窗口。您需要设置rootViewController。

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.

当您返回到开始屏幕时,再次设置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.

您可以使用transitionFromViewController(,toViewController:,duration:,options:,animations:,completion :)来设置根视图控制器的动画。

#1


13  

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

错误告诉您到底发生了什么。

Warning: Attempt to present <AppName.PauseScreen: 0x7fae61fe5ff0> on <AppName.StartScreenViewController: 0x7fae61f79980> whose view is not in the window hierarchy!

警告:尝试在 上显示 ,其视图不在窗口层次结构中! :0x7fae61fe5ff0> :0x7fae61f79980>

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

(UIApplication.sharedApplication()。delegate as AppDelegate).window?.rootViewController指向StartScreenViewController的一个实例。这很糟糕:rootViewController应该指向GameScene的一个实例。

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

根本原因必须是GameScene的呈现方式。从您的描述:

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

“StartScreenViewController”是视图控制器......然后转到“GameScene”......

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

这一定是您的问题所在。你如何从StartScreenViewController转到GameScene?

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

我的猜测是你要为应用程序添加一个新窗口。您需要设置rootViewController。

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.

当您返回到开始屏幕时,再次设置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.

您可以使用transitionFromViewController(,toViewController:,duration:,options:,animations:,completion :)来设置根视图控制器的动画。