在 SKScene 中转至 UIViewController [英] Segue in SKScene to UIViewController
问题描述
在我的 GameScene.swift 文件中,我试图像这样执行回到我的菜单视图控制器的 segue:
In my GameScene.swift file, I am trying to perform a segue back to my Menu View Controller like so:
func returnToMainMenu(){
var vc: UIViewController = UIViewController()
vc = self.view!.window!.rootViewController!
vc.performSegueWithIdentifier("menu", sender: vc)
}
该方法在节点被点击时运行:
This method is run when a node is tapped:
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:
Where menuBack
is the button back to the menu. Every time I run this code, I am thrown an NSException:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<ProxyBlock.Menu: 0x165a3e90>) has no segue with identifier 'menu''
我检查了我的 segue 标识符,它确实是菜单".
I checked my segue's identifier and it was indeed "menu".
推荐答案
你正在调用根 viewController 上的 segue.我认为这就是问题所在.您需要在场景的 viewController 上调用 segue(我假设您已经创建了 segue,因此在根 viewController 上找不到它).
You are calling the segue on the root viewController. I think that is the problem. You need to call the segue on the scene's viewController instead (where I am assuming you have created the segue, hence it is not being found on the root viewController).
现在的问题是 SKScene 不能直接访问它的 viewController,而只能访问它所在的视图.您需要手动创建指向它的指针.这可以通过为 SKScene 创建一个属性来完成:
Now the problem is that an SKScene does not have direct access to it's viewController, but just the view in which it is contained. You need to create a pointer to it manually. This can be done by creating a property for the SKScene:
class GameScene: SKScene {
weak var viewController: UIViewController?
...
}
然后,在 viewController 类中,就在 skView.presentScene(scene)
Then, in the viewController class, just before skView.presentScene(scene)
scene.viewController = self
现在,您可以直接访问 viewController.只需在这个 viewController 上调用 segue:
Now, you can access the viewController directly. Simply call the segue on this viewController:
func returnToMainMenu(){
viewController?.performSegueWithIdentifier("menu", sender: vc)
}
这篇关于在 SKScene 中转至 UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!