向窗口层次结构添加视图 [英] Adding a view to the window hierarchy

查看:19
本文介绍了向窗口层次结构添加视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的游戏中创建一个暂停屏幕.我在我的故事板中添加了一个PauseScreen"视图控制器,故事板 ID 和恢复 ID 设置为PauseScreenID"并移动到我在GameScene"中创建的函数的暂停屏幕:

func pauseSceenTransition(){让 viewController = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("PauseScreenID") 作为 UIViewController让 currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate)currentViewController.window?.rootViewController?.presentViewController(viewController,animated:false,completion:nil)}

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

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

StartScreenViewController"是我的开始屏幕的视图控制器,也是初始视图控制器.然后它转到GameScene",这是PauseScreen"需要去的地方.如果我将初始视图控制器设置为GameViewController"/GameScene",它会起作用,因此我认为我需要更改第二行:

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

以便它在GameViewController"上显示PauseScreen",而不是在StartScreenViewController"上,但我不知道该怎么做.

解决方案

错误告诉你到底发生了什么.

警告:尝试呈现 在 <AppName.StartScreenViewController: 0x7fae61f79980>其视图不在窗口层次结构中!

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

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

<块引用>

StartScreenViewController"是视图控制器……然后转到GameScene"……

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

我的猜测是您正在向应用程序添加一个新窗口.您需要改为设置 rootViewController.

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

当您返回开始屏幕时,您再次设置rootViewController.

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

<小时>

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

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" :

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!

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:

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.

解决方案

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!

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

这篇关于向窗口层次结构添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆