Detect which button is pressed with an UIButton Array(使用 UIButton 数组检测按下了哪个按钮)
问题描述
我有一个这样的 UIButton 数组:
I have an UIButton array like this:
@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *btn_Impact_Collection;
我有这个功能:
- (IBAction)impactAction:(id)sender;
在 XIB 文件中,我有九个按钮,每个按钮都通过引用插座集合连接到 btn_Impact_Collection 数组.此外,每个按钮的 Touch_inside 属性都连接到 ImpactAction 函数.
In the XIB file I have nine button, each button is connected to btn_Impact_Collection Array with the Referencing Outlet Collection. Moreover the Touch_inside property of each button is connected to the function ImpactAction.
现在,当单击按钮时,会调用 ImpactAction 函数,但在此函数内部,我如何知道按下了哪个按钮?
Now, when a button is clicked the ImpactAction function is called, but inside this function, how can i know which button is pressed?
提前感谢您的回答!
推荐答案
将 sender 转换为 UIButton 类,这将为您提供单击按钮的实例.我没有 Xcode,但有类似的东西:
Cast sender to UIButton class, and that will give you the instance of the clicked button. I don't have Xcode with me but something like:
if ([sender isMemberOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)sender;
// Then you can reference the title or a tag of the clicked button to do some further conditional logic if you want.
if([btn.currentTitle isEqualToString:@"title of button"])
{
// do something.
}
else if(etc...)
}
这篇关于使用 UIButton 数组检测按下了哪个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 UIButton 数组检测按下了哪个按钮


基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01