点击后,UIButton得到了他的默认文本

时间:2012-03-21 16:36:44

标签: objective-c ios uibutton

当我在界面构建器上创建我的UIButton时,我放了一个像Button 1这样的文本。

当我启动应用程序时,我以编程方式将文本更改为其他类似的内容:

btn1.titleLabel.text=@"Hello";

在按下按钮之前一切正常,它的默认文本是Button 1

我的目的是,如果文本已经以编程方式更改,则当我按下按钮时,它无法获取其默认文本值,我的意思是文本Hello应该保留。我该怎么办? Thanx提前:))

3 个答案:

答案 0 :(得分:6)

不要直接更改textLabel,请使用setTitle:forState:而不是......

See UIButton reference

答案 1 :(得分:3)

[btn1 setTitle:@"Hello" forState:UIControlStateNormal];
[btn1 setTitle: @"Hello" forState: UIControlStateSelected];
[btn1 setTitle: @"Hello" forState: UIControlStateHighlighted];

希望这有帮助。

答案 2 :(得分:2)

[myButton setTitle: @"myTitle" forState: UIControlStateNormal];

use  UIControlStateNormal to set your title.

there are couple of states that UIbuttons provide, you can have a look:

[myButton setTitle: @"myTitle" forState: UIControlStateApplication];
[myButton setTitle: @"myTitle" forState: UIControlStateHighlighted];
[myButton setTitle: @"myTitle" forState: UIControlStateReserved];
[myButton setTitle: @"myTitle" forState: UIControlStateSelected];
[myButton setTitle: @"myTitle" forState: UIControlStateDisabled];