How to define which button pressed if they both have same IBAction?(如果它们都具有相同的 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,如何定义按下哪个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果它们都具有相同的 IBAction,如何定义按下哪个按钮?
基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01