Reputation: 55
I'm really new to coding, so if this is stupid please be gentle :P.
What I'm trying to achieve with swift coding is to have 2 view controllers that can pass data based on the sending segue.
VC1 is setup with 2 labels and 2 buttons. Label 1 is player 1 name, label 2 is player 2 name. Button 1 is to choose player 1 from a global list of names in VC2 and button 2 is the same for player 2
VC2 has a picker view set up with a list of names and a button to "choose" the name chosen in the pickerView.
I know how to set up all the buttons, pickers, and labels and even how to send the data back with a prepareForSegue, however what i don't know how to do is tell the VC2 "choose" button to send it to playerOneSegue or playerTwoSegue based on which button was chosen in VC1.
Any help will be appreciated, again I'm sorry if this is stupid but I've been stuck for a while now and haven't been able to find anything online to help. I'm not even sure if it's the way I should be doing this. Half of me wants to just set up an alert for each button to not even jump to the other VC, but the other part wants to figure out how to do this because I'm sure there must be a way lol.
Upvotes: 5
Views: 5980
Reputation: 6775
If you have two segues to same VC, you can give each segue unique identifier, and distinguish between the senders based on identifiers. Set tag for buttons (say 1 for button1 and 2 form button2)
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "firstIdentifier") {
var VC2 : VC2 = segue.destinationViewController as VC2
VC2.buttonTag = sender.tag
}
if (segue.identifier == "secondIdentifier") {
var VC2 : VC2 = segue.destinationViewController as VC2
VC2.buttonTag = sender.tag
}
}
In VC2, declare a variable buttonTag
var buttonTag = 0
and wherever you are performing the segue in VC2, just check for the value of buttonTag
if buttonTag == 1 {
//segue caused by button1 of VC1
}
else
if buttonTag == 2 {
//segue caused by button2 of VC1
}
else {
//segue caused by something else
}
I hope that is what you are trying to achieve
Upvotes: 7
Reputation: 2346
Not quite sure exactly what you are asking. But it sound like you want to send data from vc1 to vc2 which then decided which segue to run in vc2.
What I would do: in vc1 prepareForSegue i would set a property in vc2 which said something about what to do with the result in vc2 based on the button in vc1.
in your prepareForSegue:
if segue.identifier == "MyVc2Segue" {
let yourNextViewController = (segue.destinationViewController as CommentViewController)
// Set variable
yourNextViewController.myVar = somethingVar
}
Then in your vc2 I would use a outlet instead of relying on segues, and in the outlet take a stance/decision on which segue you should perform, based on the variable mentioned above.
Upvotes: 0