简体   繁体   中英

Segue in SKScene to UIViewController

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()
            }
        }
    }
}

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 checked my segue's identifier and it was indeed "menu".

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).

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?
    ...
}

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

scene.viewController = self

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

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

How to segue from Scene to ViewController

Swift 3 - Works with SpriteKit / UIKit

You can use NSNotification .

Example:

1.) Create a segue in the storyboard and name the identifier "segue"

2.) Create a function in the ViewController you are segueing from.

func goToDifferentView() {

    self.performSegue(withIdentifier: "segue", sender: self)

}

3.) In the ViewDidLoad() of your ViewController you are segueing from create the observer.

NotificationCenter.default.addObserver(self, selector: #selector(goToDifferentView), name: "segue" as NSNotification.Name, object: nil)

4.) In the ViewController or Scene you are segueing to, add the Post Method wherever you want the segue to be triggered.

NotificationCenter.default.post(name: "segue" as NSNotification.Name, object: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM