您可以使用标记值来确定 Taped Button,假设您有树按钮,例如:-
@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;
@property (nonatomic, strong) UIButton *btn3;
然后设置按钮标签,如: -
btn1.tag=1;
btn2.tag=2;
btn3.tag=3;
IBAction
并为每个按钮设置相同,并且:-
[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];
}
}
信息
如果您正在使用id
,IBAction
那么您将获得如下按钮对象:-
- (IBAction)difficultyButtonPressed:(id)sender {
UIButton *button = (UIButton *)sender;
NSLog(@"Button tag is %d",button.tag);
}