如何从 UIButton 获取名称? [英] How do I get the name from a UIButton?

查看:71
本文介绍了如何从 UIButton 获取名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 xcode 的新手,我正在尝试制作一个通用的按钮按下方法.我希望它检查按钮的名称(而不是标题),然后我想做一些字符串操作来设置一些值.

I am new to xcode, and I am trying to make a generic button press method. I want it to check the name (not the title) of the button and then I want to do some string manipulation to set some values.

我的 ViewController.h 中有一个按钮设置

I have a button setup in my ViewController.h

@property (strong, nonatomic) IBOutlet UIButton *button_1_3;

然后我的 ViewController.m 中有一个通用方法

Then I have a generic method in my ViewController.m

- (IBAction)buttonPress:(id)sender
{
    UIButton *bSender = (UIButton *)sender;

    self.char1Text1.text = bSender.titleLabel.text; // this is the bit that I am not getting
}

所以我的一堆按钮上会有相同的文字,我想看到的是button_1_3".我希望有一个bSender.name"选项,但我还没有找到.

So a bunch of my buttons are going to have the same text on them, what I want to see is the "button_1_3". I was hoping there was a "bSender.name" option, but I am yet to find it.

任何帮助将不胜感激

推荐答案

给所有按钮一个标签(这也可以在 Interface Builder 中完成)

Give all your buttons a tag (this can also be done in Interface Builder)

button_1_0.tag = 0;
button_1_1.tag = 1;
button_1_2.tag = 2;
button_1_3.tag = 3;

在您的 buttonPress: 方法中检查 tag 并显示不同的文本

In your buttonPress: method check the tag and show a different text

- (IBAction)buttonPress:(id)sender
{
    UIButton *bSender = (UIButton *)sender;
    switch (bSender.tag) {
        case 0:
            self.char1Text1.text = @"first text";
            break;
        case 1:
            self.char1Text1.text = @"second text";
            break;
        case 2:
            self.char1Text1.text = @"thrid text";
            break;
        case 3:
            self.char1Text1.text = @"fourth text";
            break;
        default:
            break;
    }
}

这篇关于如何从 UIButton 获取名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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