swift – 将视图添加到窗口层次结构中
我想在我的游戏上创建一个暂停屏幕.我在故事板中添加了一个’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) } 但是当它被调用时,我得到错误: 警告:尝试显示< AppName.PauseScreen:0x7fae61fe5ff0> on< AppName.StartScreenViewController:0x7fae61f79980>其视图不在窗口层次结构中! “StartScreenViewController”是我的开始屏幕的视图控制器,是初始视图控制器.然后转到“GameScene”,这是“PauseScreen”需要去的地方.如果我将初始视图控制器设置为“GameViewController”/“GameScene”,它可以工作,所以我认为我需要更改第二行: let currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate) 所以它在“GameViewController”上呈现“PauseScreen”,而不是“StartScreenViewController”,但我不知道该怎么做.
错误告诉您到底发生了什么.
警告:尝试显示< AppName.PauseScreen:0x7fae61fe5ff0> on< AppName.StartScreenViewController:0x7fae61f79980>其视图不在窗口层次结构中! (UIApplication.sharedApplication().delegate as AppDelegate).window?.rootViewController指向StartScreenViewController的一个实例.这很糟糕:rootViewController应该指向GameScene的一个实例. 根本原因必须是GameScene的呈现方式.从您的描述:
这一定是您的问题所在.你如何从StartScreenViewController转到GameScene? 我的猜测是你要为应用程序添加一个新窗口.您需要设置rootViewController. let gameScene = UIStoryboard(name: "Main",bundle:nil).instantiateViewControllerWithIdentifier("GameSceneID") as UIViewController let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) appDelegate.window?.rootViewController = gameScene 当您返回到开始屏幕时,再次设置rootViewController. let initialViewController = UIStoryboard(name: "Main",bundle:nil).instantiateInitialViewController() as UIViewController let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) appDelegate.window?.rootViewController = initialViewController 您可以使用 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |