Search code examples
iosswiftuiviewcontrollersegueuistoryboardsegue

Switching between View Controllers (with simultaneous VCs)


Currently, I have a main ViewController that calls a GameViewController to load a SKScene in front of the main ViewController. I do this in the viewDidLoad of the main ViewController via:

let gameView = self.storyboard?.instantiateViewController(withIdentifier: "Game") as! GameViewController
let top: UIViewController? = UIApplication.shared.keyWindow?.rootViewController
top?.present(gameView, animated: false, completion: nil)

This currently works, I have SKScene displayed on top of a ViewController that displays an image (with an ImageView) in the background.

However, now I want to switch to another ViewController, and I'm not sure how to do this. Things I have tried:

  • Segues (calling them with performSegue(withIdentifier: "mySegueID", sender: nil) ) NOT WORKED

  • Push the new ViewController (with navigationController?.pushViewController(nextViewController, animated: true)) NOT WORKED

  • Present the new ViewController (using the same code I use to display the GameViewController)

THIS 'PRESENT VIEWCONTROLLLER' WORKED. However, after I called the next ViewController then dismissed it via a segue triggered by a button push in the new ViewController, the new ViewController wouldn't call again with the 'present ViewController' method anymore.

Any ideas on how to successfully switch between ViewControllers (with one of them having a GameViewController simultaneously)?


Solution

  • I wonder how segues didn't work for u? You need to give segue an identifier and then call it in some function or action(like buttonPressed) or somth. in performSegue(withIdentifier: "urSegueIdentifier", sender: Any?.self) i'm not sure if sender can be nil, maybe try .self?