在SKScene中使用UIViewController [英] Segue in SKScene to UIViewController

查看:584
本文介绍了在SKScene中使用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''

I检查了我的segue的标识符,它确实是菜单。

I checked my segue's identifier and it was indeed "menu".

推荐答案

您正在根视图控制器上调用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 {
    var viewController: UIViewController?
    ...
}

然后,在viewController类中,就在之前 skView.presentScene(场景)

Then, in the viewController class, just before skView.presentScene(scene)

scene.viewController = self

现在,您可以直接访问viewController。简单地调用SEGUE有关此的viewController:

Now, you can access the viewController directly. Simply call the segue on this viewController:

func returnToMainMenu(){
    self.viewController.performSegueWithIdentifier("menu", sender: vc)
}

这篇关于在SKScene中使用UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆