在我的GameScene.swift文件中,我试图像下面这样对我的菜单视图控制器执行一次segue:
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() } } } }
menuBack返回菜单的按钮在哪里。每次运行此代码时,都会引发NSException:
menuBack
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)
skView.presentScene(scene)
scene.viewController = self
现在,您可以直接访问viewController。只需在此viewController上调用segue:
func returnToMainMenu(){ viewController?.performSegueWithIdentifier("menu", sender: vc) }