Feindpak
Feindpak

Reputation: 45

switch to view controller from GameScene.swift

I have a View Controller called "MenuVC" in my Main.Storyboard file. I want to show this View Controller programmatically from my GameScene.swift file.

Upvotes: 0

Views: 1382

Answers (2)

Bilal
Bilal

Reputation: 19156

First set the view controller storyboard id in storyboard.

enter image description here

And then

let menuVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MenuVC" as! MenuVC

And if you are using navigation controller

self.navigationController?.pushViewController(menuVC, animated: true)

Or you can present it.

self.present(menuVC, animated: true, completion: nil)

Upvotes: 0

user6338195
user6338195

Reputation:

Somewhere in GameScene.swift file:

//...
let vc = self.storyboard?.instantiateViewController(withIdentifier: "MenuVC") as! MenuVC
self.present(vc, animated: true, completion: nil)
//...

But here is the full answer: Swift programmatically navigate to another view controller/scene

Upvotes: 1

Related Questions