Walker
Walker

Reputation: 145

Programmatically triggering unwind segue

I am making an app that segues to another screen on a "Go" button pressed. After 3 seconds, the screen turns black. Then 6 seconds later I want it to unwind back to the main screen without having to click a button, but so far I don't know how to get this to work.

I've ctrl dragged from the view controller to the exit button to create the unwind segue, but I don't know how to call it in code.

Here's the code I have in my GameViewController related to the segue:

import UIKit import SpriteKit import GameplayKit

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let skView = view as! SKView
    let scene = GameScene(size: view.bounds.size)

    //skView.showsFPS = true
    scene.scaleMode = .resizeFill
    scene.vc = self
    skView.presentScene(scene)
}

@IBAction func unwindToMainMenu(sender: UIStoryboardSegue)
{

    //let gameVeiwController = sender.source]
    // Pull any data from the view controller which initiated the unwind segue.
}

Now here is my GameScene class, containing the function which I want to unwind the segue:

class GameScene: SKScene {

var vc: UIViewController?

private var circleNode : SKShapeNode?

var fingersDown: Int = 0
let maxFingers: Int = 4
var selectionStarted = false

var touchNodes = [UITouch:SKShapeNode]()

func startSelectionGame(){
    selectionStarted = true
    self.backgroundColor = UIColor.yellow
    _ = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { timer in
        //both dont work
        self.vc!.performSegue(withIdentifier: "unwindToMainMenu", sender: self.vc)
        self.vc?.performSegue(withIdentifier: "unwindToMainMenu", sender: self)
    }
    _ = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false) { timer in
        self.backgroundColor = UIColor.black
        self.changeColors()
    }

}

The Main.storyboard just has a segue from the first screen to the game screen.

I just want to get my program working, I know I'm missing something simple. Thanks.

Upvotes: 0

Views: 1283

Answers (2)

Noel Downs
Noel Downs

Reputation: 1

Instead of an unwind segue you could use:

dismiss(animated: true)

Upvotes: 0

vacawama
vacawama

Reputation: 154583

You need to do 3 things:

  1. Find your segue in the Document Outline view on the left. Click on it.
  2. In the Attributes Inspector on the right, set the Identifier to unwindToMainMenu.
  3. Call self.performSegue(withIdentifier: "unwindToMainMenu", sender: self) when you want to trigger it.

Upvotes: 3

Related Questions