ctd*_*ers 4 ios segue sprite-kit swift
在我的GameScene.swift文件中,我正在尝试将segue执行回我的菜单视图控制器,如下所示:
func returnToMainMenu(){
var vc: UIViewController = UIViewController()
vc = self.view!.window!.rootViewController!
vc.performSegueWithIdentifier("menu", sender: vc)
}
Run Code Online (Sandbox Code Playgroud)
点击节点时会运行此方法:
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()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
哪里menuBack是按钮返回菜单.每次运行此代码时,都会抛出NSException:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<ProxyBlock.Menu: 0x165a3e90>) has no segue with identifier 'menu''
Run Code Online (Sandbox Code Playgroud)
我检查了我的segue的标识符,它确实是"菜单".
您正在根viewController上调用segue.我认为这就是问题所在.你需要在场景的viewController上调用segue(我假设你创建了segue,因此它没有在根viewController上找到).
现在问题是SKScene没有直接访问它的viewController,而只是它包含它的视图.您需要手动创建指向它的指针.这可以通过为SKScene创建属性来完成:
class GameScene: SKScene {
var viewController: UIViewController?
...
}
Run Code Online (Sandbox Code Playgroud)
然后,在viewController类中,就在之前 skView.presentScene(scene)
scene.viewController = self
Run Code Online (Sandbox Code Playgroud)
现在,您可以直接访问viewController.只需在此viewController上调用segue:
func returnToMainMenu(){
self.viewController.performSegueWithIdentifier("menu", sender: vc)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5028 次 |
| 最近记录: |