如果它们都具有相同的 IBAction,如何定义按下哪个按钮? [英] How to define which button pressed if they both have same IBAction?

查看:22
本文介绍了如果它们都具有相同的 IBAction,如何定义按下哪个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 UIButtons(我使用 IB 创建它们),它们使用相同的 IBAction 连接到 File 的所有者,我如何定义它们中的哪一个被按下?

I have two UIButtons (I create them using IB), which connected to File's owner with the same IBAction, how can i define which of them are pressed?

推荐答案

你的动作可以这样实现:

Your action can be implemented like this:

- (IBAction) buttonTapped: (id) sender
// you can also replace id with UIButton*

然后在这个方法里面你可以通过 -isEqual: 方法检查

Then inside this method you can check by -isEqual: method

- (IBAction) buttonTapped: (id) sender
{
   if ([sender isEqual:referenceToOneOfYourButtons]) {
   // do something
   }
   else if ([sender isEqual:referenceToTheOtherButton]) {
   ...
   }
}

或者,您可以设置不同的值来标记按钮的属性,然后:

Alternatively you can set up different values to tag property of buttons and then:

- (IBAction) buttonTapped: (UIButton*) sender
{
   const int firstButtonTag = 101;
   const int otherButtonTag = 102;

   if (sender.tag == firstButtonTag) {
   ...
   }
   else if (sender.tag == otherButtonTag) {
   ...
   }
}

您需要在 .xib 或代码中设置此标记.

You need to set up this tag either in your .xib or in code.

这篇关于如果它们都具有相同的 IBAction,如何定义按下哪个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆