使用 UIButton 数组检测按下哪个按钮
我有一个像这样的 UIButton 数组:
@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *btn_Impact_Collection;
我有这个函数:
- (IBAction)impactAction:(id)sender;
在 XIB 文件中,我有九个按钮,每个按钮都通过引用插座集合连接到 btn_Impact_Collection 数组。此外,每个按钮的 Touch_inside 属性都连接到函数 ImpactAction。
现在,当单击按钮时,将调用 ImpactAction 函数,但在该函数内,我如何知道按下了哪个按钮?
预先感谢您的回答!
I have an UIButton array like this:
@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *btn_Impact_Collection;
and I have this function:
- (IBAction)impactAction:(id)sender;
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.
Now, when a button is clicked the ImpactAction function is called, but inside this function, how can i know which button is pressed?
Thanks in advance for the answer!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 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:
在界面生成器 (1-9) 中为每个按钮设置标签,然后
对所有其他按钮说同样,或者您可以将其放入开关中。
Set tags for each button in interface builder (1-9), then say
And the same for all the others, or you could put it in a switch.
您可以:
Rather than do a string check on the title (which is slow and can be tedious) you can:
另一种选择..强制发送者检查它是否是 UIButton,然后切换 sender.tag:
Another option.. Cast sender to check if it's a UIButton, then switch sender.tag: