我有三个按钮,它们都做同样的事情,执行 segue。所有链接到同一个连接。
- (IBAction)difficultyButtonPressed:(id)sender {
// Any difficulty selected
[self performSegueWithIdentifier:@"mainGameTurnGuess" sender:self];
}
我需要做的是确定在 prepareForSegue
方法中按下了哪个按钮。如何判断按下了三个按钮中的哪一个。
无需查看按钮上的措辞/文本,因为这会因本地化而改变。
最佳答案
假设您有树形按钮,您可以使用标签值确定 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;
并为每个 Button 设置 Same 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);
}
关于ios - 如何确定三个按钮中按下了哪个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19281803/