0

Possible Duplicate:
Passing variables between view controllers

Let's say I have 3 buttons in the same ViewController(FirstController) with tags from 1-3. All three buttons opened a new ViewController(SecondController). But how can I, in the SecondController, check what button was pressed to get here, or maybe what segue was used? So the SecondController can perform different depending on which button was pressed to open it. I was maybe thinking about a switch and case.

Community
  • 1
  • 1
Olaknorr
  • 53
  • 1
  • 6

2 Answers2

1

There are 2 possible ways of doing this.

One way is to assign a unique segue to each button. On the method prepareForSegue: you identify the button and give that information to the destinationViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Button1Segue"]) {
        [segue.destinationViewController setButton:1];
    }

    if ([segue.identifier isEqualToString:@"Button2Segue"]) {
    ...
}

The other way would be to create a property where you store the last pressed button.

- (IBAction)buttonTap:(id)sender {

    self.lastPressedButton = sender.tag;
}

and in prepareForSegue: you would do:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"SecondControllerSegue"]) {
        [segue.destinationViewController setButton: self.lastPressedButton];
    }
}

Hope it helps

pedros
  • 1,197
  • 10
  • 18
0
if (button.tag == 0) {
   NSLog(@"Button one.");
} else if (button.tag == 1) {
   NSLog(@"Button two.");
}

You would put something like this in the method that occurs just before the action of your three buttons. That could be viewWillDisappear, prepareForSegue, etc.

Best of luck!

jakenberg
  • 2,125
  • 20
  • 38