Once my SpriteKit game is over, I would like to return to my UIKit MenuViewController . From what I have learned so far, using a protocol / delegate is the best (?) Option, but I have not been able to get this to work. I know that the protocol will probably go above the class declaration for GameViewController and will look something like this:
protocol GameViewControllerDelegate { var gameOver: Bool? }
But I need help to access this from GameScene and make it reject the GameViewController . Below are the bones of the app if this helps.

MenuViewController
class MenuViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } @IBAction func goToGame(_ sender: UIButton) { performSegue(withIdentifier: "toGameSegue", sender: sender.currentTitle) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let destinationVC = segue.destination as? GameViewController { if let item = sender as? String { destinationVC.numberOfPlayers = item } } } }
Gameviewcontroller
class GameViewController: UIViewController { var numberOfPlayers: String? override func viewDidLoad() { super.viewDidLoad() if let view = self.view as! SKView? { if let scene = SKScene(fileNamed: "GameScene") { scene.scaleMode = .aspectFill scene.userData = NSMutableDictionary() scene.userData?.setObject(numberOfPlayers!, forKey: "numberOfPlayers" as NSCopying) view.presentScene(scene) } } } ...
Gamescene
class GameScene: SKScene { var howManyPlayers: String? override func didMove(to view: SKView) { if let numPlayers = self.userData?.value(forKey: "numberOfPlayers") { howManyPlayers = numPlayers as? String } print(howManyPlayers!) } ...
This SpriteKit game has a MenuViewController, GameViewController, and GameScene. When you press the button from the MenuViewController, the data is sent via segue to the GameViewController. Before the GameViewController presents a GameScene, it stores the data in the userData variable of the scene so that GameScene can access it. In this example, this is the number of players.
source share