Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple segues to the same view controller

Tags:

ios

swift

segue

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.

like image 282
user3763291 Avatar asked Mar 23 '15 19:03

user3763291


1 Answers

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

like image 89
saurabh Avatar answered Nov 14 '22 23:11

saurabh