就可以使用標記值ditermine丈量按鈕假設你有一個實例樹按鈕: -
@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];
}
}
對於信息
如果您使用的是IBAction
id
那麼你得到的按鈕對象,如: -
- (IBAction)difficultyButtonPressed:(id)sender {
UIButton *button = (UIButton *)sender;
NSLog(@"Button tag is %d",button.tag);
}
爲每個添加不同的「標籤」值。 – Wain
我有,但我如何在prepareForSegue中引用它們 – StuartM
創建它們時,請保留對按鈕的強烈參考 – LuisCien