Segue in SKScene to UIViewController

前端 未结 2 1703
一个人的身影
一个人的身影 2020-12-01 10:02

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 = U         


        
2条回答
  •  一个人的身影
    2020-12-01 10:35

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

提交回复
热议问题