0
votes

I am working to implement code that will move between 2 View Controllers, vc1 and vc2.

vc1 toggles between 2 SKViews, and vc2 just displays some simple things in UIView.

vc1 and vc2 are both in the main storyboard.

vc1 is displayed first, then at a point in an SKScene it segues to vc2:

let currentViewController: UIViewController = (self.view?.window?.rootViewController)!
currentViewController.performSegue(withIdentifier: "VC1toVC2", sender: currentViewController)

After a short delay in vc2, it segues back to vc1:

self.performSegue(withIdentifier: "VC2toVC1", sender: self)

These segues work.

Then, with the same code as above (using the same segue):

let currentViewController: UIViewController = (self.view?.window?.rootViewController)!
currentViewController.performSegue(withIdentifier: "VC1toVC2", sender: currentViewController)

...the 2nd time it does not work to segue from vc1 to vc2. There's no exception, just that vc2 does not display.

I found some other posts that don't address the issue, for example: * Swift segue not working? - suggests that UI updating has to be in main thread, and this one that shows how to do that in Swift 3 - How do I dispatch_sync, dispatch_async, dispatch_after, etc in Swift 3? --- I get the same result * other posts suggested using the view controller's .show or .present method --- I get the same result

Any thoughts/guidance?

1

1 Answers

0
votes

I solved this using a delegate as other posts have mentioned, eg How to call method from ViewController in GameScene. The delegate allows the SKScene object to call / run code in its View Controller. This allows the View Controller to perform the segue instead of trying to perform the segue from the SKScene object (not sure that's even possible, to perform the segue from the SKScene object). Here's a simple tutorial as well https://useyourloaf.com/blog/quick-guide-to-swift-delegates/ if you're not all that familiar with delegates in Swift. I can post more details if it would help anyone.