Arthur Macpherson Arthur Macpherson - 5 months ago 53
iOS Question

Swift 3 Check which segue was used

I have created a game where at first a home screen is presented to the user. If the user selects play game the buttons and background disappear and the game begins in the form of an SKScene. Then when the player dies there is an automatic segue to a second view controller. There are then two options in the form of buttons for the user. Either Menu or Restart. Both segue's lead to the same view controller. How can i differentiate between the two segues so that in the first view controller it will resort or present the menu accordingly.


Assuming that you set an identifier for each of your segue, you can check which segue will be performed by prepare(for segue: UIStoryboardSegue, sender: Any?) method, implement it in the ViewController and check the segue.identifier:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "firstSegue" {
      // .. do somtheing
    } else if segue.identifier == "secondSegue" {
        // .. do something

Hope this helped.