ios - 如何确定三个按钮中按下了哪个按钮

标签 ios ios6 uibutton uistoryboardsegue

我有三个按钮,它们都做同样的事情,执行 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/

相关文章:

ios - 如何将 5 个不同的头像应用到 5 个不同的 UIButton?

iphone - iOS6 iPad运行应用数量会影响内存警告频率吗?

swift - 如何防止 UIButton 的图像颜色重置为默认值?

ios - 处理按钮状态

iOS 7,导航栏没有半透明......为什么更多选项卡看起来是半透明的?

ios - 如何检查用户是否已经下载了非消耗品Inapp购买

ios - UITableView 宽度动画时 UITableViewCell 中的 UIButton 跳转

ios6 - UICollectionViewController 编辑

ios - 在 iOS 6 中旋转以触发 Segue

ios - 将 UIButton 传递给函数