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.
Asked
Active
Viewed 3,389 times
3
-
1`prepare(for segue: )` is the thing you are looking for : http://stackoverflow.com/a/39838364/3411787 – Mohammad Zaid Pathan Oct 20 '16 at 14:49
2 Answers
4
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.

Ahmad F
- 30,560
- 17
- 97
- 143
-
1@Arthur Just don't forget to set `firstSegue` and `secondSegue` as identifiers for your segues in storyboard. – alexburtnik Oct 20 '16 at 14:55
-
@alexburtnik that's right, I mentioned that "Assuming that you set an identifier for each of your segue..." – Ahmad F Oct 20 '16 at 15:00
-
If i sent a variable x in the if statement where the value of it changed depending on which if statement it used what code would i use to send it to the first view – Arthur Macpherson Oct 20 '16 at 15:01
1
Your segues should have identifiers. In your prepareForSegue function Check segue.identifier to determine what to do next. For example, in swift 3:
func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?)
{
if segue.identifier == "menuSegue"
{
// do menu
}
else if segue.identifier == "restartSegue"
{
// do restart
}
}

Lexo
- 412
- 4
- 6