我正在尝试在游戏中创建暂停屏幕。我添加了一个' PauseScreen'我的故事板中的viewController,故事板ID和恢复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)
以便它呈现" PauseScreen"在" GameViewController"上,而不是在" StartScreenViewController"但我不知道该怎么做。
答案 0 :(得分:13)
错误告诉你到底发生了什么。
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
指向StartScreenViewController
的实例。这很糟糕:rootViewController
应该指向GameScene
。
根本原因必须是GameScene
的呈现方式。从您的描述:
“StartScreenViewController”是视图控制器......然后转到“GameScene”......
这一定是你的问题所在。你如何从GameScene
转到StartScreenViewController
?
我的猜测是你要为应用程序添加一个新窗口。您需要设置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
您可以使用transitionFromViewController(,toViewController:, duration:, options:, animations:, completion:)
动画设置根视图控制器。