使用 UIButton 数组检测按下哪个按钮

发布于 2024-11-25 20:10:29 字数 413 浏览 1 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝眸 2024-12-02 20:10:29

将 sender 转换为 UIButton 类,这将为您提供单击按钮的实例。我没有 Xcode,但有类似的东西:

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...)
}

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...)
}
鱼窥荷 2024-12-02 20:10:29

在界面生成器 (1-9) 中为每个按钮设置标签,然后

if ([sender tag] == 1) {
//First button was pressed, react.
}
else if ([sender tag] == 2) {
//Second button was pressed, react.
}
// Etc...
else {
//Last button was pressed, react.
}

对所有其他按钮说同样,或者您可以将其放入开关中。

Set tags for each button in interface builder (1-9), then say

if ([sender tag] == 1) {
//First button was pressed, react.
}
else if ([sender tag] == 2) {
//Second button was pressed, react.
}
// Etc...
else {
//Last button was pressed, react.
}

And the same for all the others, or you could put it in a switch.

久而酒知 2024-12-02 20:10:29

您可以:

- (void)buttonPressed:(id)sender
{
    for( UIButton *button in self.buttonCollection )
    {
        if( sender == button )
        {
            // sender is your button (eg. you can access its tag)
        }
    }
}

Rather than do a string check on the title (which is slow and can be tedious) you can:

- (void)buttonPressed:(id)sender
{
    for( UIButton *button in self.buttonCollection )
    {
        if( sender == button )
        {
            // sender is your button (eg. you can access its tag)
        }
    }
}
凉风有信 2024-12-02 20:10:29

另一种选择..强制发送者检查它是否是 UIButton,然后切换 sender.tag:

if ([sender isMemberOfClass:[UIButton class]]) {
        switch ([sender tag]) {
            case 0:
                //do stuff for button with tag 0
                break;

            case 1:
                //do stuff for button with tag 1
                break;

            case 2:
                ....
                break;

            default:
                break;
        }
    }

Another option.. Cast sender to check if it's a UIButton, then switch sender.tag:

if ([sender isMemberOfClass:[UIButton class]]) {
        switch ([sender tag]) {
            case 0:
                //do stuff for button with tag 0
                break;

            case 1:
                //do stuff for button with tag 1
                break;

            case 2:
                ....
                break;

            default:
                break;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文