Segue in SKScene to UIViewController

前端 未结 2 1701
一个人的身影
一个人的身影 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:43

    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)
    

提交回复
热议问题