简体   繁体   English

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

[英]How to determine which button was pressed out of three buttons

I have three buttons, they all do the same thing perform a segue. 我有三个按钮,他们都做同样的事情执行一个segue。 All linked to the same connection. 所有都链接到相同的连接。

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

What I need to do is determine what button was pressed in the prepareForSegue method. 我需要做的是确定prepareForSegue方法中按下了什么按钮。 How can I tell which out of the three buttons are pressed. 如何判断按下三个按钮中的哪一个。

Without looking at the wording/text on the button as this would change for localisation. 不看按钮上的措辞/文字,因为这会改变本地化。

You can ditermine the Taped Button using Tag Value suppose you have tree Button for Example:- 你可以使用Tag Value来设置Taped Button,假设你有树按钮例如: -

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

Then set Tag of Button like:- 然后设置按钮标签,如: -

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

and set Same IBAction for each Button and:- 并为每个按钮设置相同的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]; 
      }

}

For Info 对于信息

If you are using id in IBAction then you get the Button Object like:- 如果你在IBAction中使用id ,那么你得到的Button对象如下: -

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

将按钮的标记存储在实例变量中,并在调用prepareForSegue时查看该变量

You can forward on the sender parameter sent to difficultyButtonPressed: as that is the button that was pressed. 您可以转发发送到difficultyButtonPressed:sender参数difficultyButtonPressed:因为这是按下的按钮。 That is, if you do this: 也就是说,如果你这样做:

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

the sender parameter sent to prepareForSegue:sender: will be the button that was pressed. 发送给prepareForSegue:sender:sender参数prepareForSegue:sender:将是按下的按钮。

You can also use the buttons Restoration Id. 您还可以使用Restoration Id按钮。 In prepareForSegue you can then do the following: 在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
}

I quote the apple documentation: 我引用苹果文档:

Because segues can be triggered from multiple sources, you can use the information in the segue and sender parameters to disambiguate between different logical paths in your app. 由于segues可以从多个源触发,因此您可以使用segue和sender参数中的信息来消除应用中不同逻辑路径之间的歧义。 For example, if the segue originated from a table view, the sender parameter would identify the table view cell that the user tapped. 例如,如果segue源自表视图,则sender参数将标识用户点击的表视图单元格。 You could use that information to set the data on the destination view controller 您可以使用该信息在目标视图控制器上设置数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM