检测使用UIButton阵列按下哪个按钮 [英] Detect which button is pressed with an UIButton Array

查看:121
本文介绍了检测使用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?

提前感谢您的回答!

推荐答案

将发件人转换为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阵列按下哪个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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