How to determine which button was pressed from three buttons

I have three buttons, they all do the same as in segue mode. All are connected to the same connection.

- (IBAction)difficultyButtonPressed:(id)sender {
    // Any difficulty selected
    [self performSegueWithIdentifier:@"mainGameTurnGuess" sender:self];
}

What I need to do is determine which button was pressed in the method prepareForSegue. How to determine which of the three buttons is pressed.

Despite the wording / text on the button, as this will change for localization.

+4
source share
4 answers

You can click the Taped button using the Tag value. Suppose you have a tree button for an example: -

@property (nonatomic, strong)  UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;
@property (nonatomic, strong) UIButton *btn3;

Then set the Tag of Button as: -

btn1.tag=1;
btn2.tag=2;
btn3.tag=3;

and set Same for each button IBActionand: -

[btn1 addTarget:self action:@selector(difficultyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

[btn2 addTarget:self action:@selector(difficultyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

[btn3 addTarget:self action:@selector(difficultyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

- (IBAction)difficultyButtonPressed:(UIButton*)sender
{
 NSLog(@"Button tag is %d",sender.tag);

     // you can use if else condition using sender.tag  like

      if(sender.tag==1)//first button related identifire
      {
           [self performSegueWithIdentifier:@"mainGameTurnGuess_FirstButtonIdentirier" sender:sender]; 
      }
      else if(sender.tag==2)//second button related identifier
      {
            [self performSegueWithIdentifier:@"mainGameTurnGuess_secondButtonIdentirier" sender:sender]; 
      }
      else   //Third button related identifier
      {
            [self performSegueWithIdentifier:@"mainGameTurnGuess_ThirdButtonIdentirier" sender:sender]; 
      }

}

For information

id IBAction, Button : -

- (IBAction)difficultyButtonPressed:(id)sender {
    UIButton *button = (UIButton *)sender;
    NSLog(@"Button tag is %d",button.tag);
}
+8

, prepareForSegue

0

sender, difficultyButtonPressed:, , . , :

- (IBAction)difficultyButtonPressed:(id)sender {
    // Any difficulty selected
    [self performSegueWithIdentifier:@"mainGameTurnGuess" sender:sender];
}

sender, prepareForSegue:sender:, .

0

Restoration Id. prepareForSegue :

UIButton *btnSender;
if ([sender isMemberOfClass:[UIButton class]])
{
    btnSender = (UIButton *)sender;
}

// Then you can reference the Restoration Id or a tag of the clicked button to do further conditional logic if you want.
if([btnSender.restorationIdentifier isEqualToString:@"myBtn1"])
{
   //do something
}

:

segues , segue sender . , segue , , .

0

All Articles