在我的 GameScene.swift 文件中,我尝试执行返回到我的菜单视图控制器的操作,如下所示:
func returnToMainMenu(){
var vc: UIViewController = UIViewController()
vc = self.view!.window!.rootViewController!
vc.performSegueWithIdentifier("menu", sender: vc)
}
当点击节点时运行此方法:
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if gameOn == false{
if restartBack.containsPoint(location){
self.restartGame()
}
else if menuBack.containsPoint(location){
self.returnToMainMenu()
}
else if justBegin == true{
self.restartGame()
}
}
}
}
Where menuBack
是返回菜单的按钮。每次运行此代码时,都会抛出 NSException:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<ProxyBlock.Menu: 0x165a3e90>) has no segue with identifier 'menu''
我检查了我的 segue 的标识符,它确实是“菜单”。
您正在根 viewController 上调用 segue。我认为这就是问题所在。您需要在场景的 viewController 上调用 segue(我假设您已经创建了 segue,因此在根 viewController 上找不到它)。
现在的问题是 SKScene 无法直接访问它的 viewController,而只能访问它所在的视图。您需要手动创建指向它的指针。这可以通过为 SKScene 创建一个属性来完成:
class GameScene: SKScene {
weak var viewController: UIViewController?
...
}
然后,在 viewController 类中,就在之前skView.presentScene(scene)
scene.viewController = self
现在,您可以直接访问viewController。只需调用此 viewController 上的 segue:
func returnToMainMenu(){
viewController?.performSegueWithIdentifier("menu", sender: vc)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)