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,如何定义按下哪个按钮?
基础教程推荐
- Android:STATE_SELECTED不工作 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
