问题描述
所以当我点击它时,我想更新UIButton上的文本。我正在使用以下行来更改文本:
So I'm trying to update the text on a UIButton when I click it. I'm using the following line to change the text:
calibrationButton.titleLabel.text = @"Calibration";
我已经验证文本正在更改,但是当我运行应用程序,我点击按钮,它会分割为校准,然后再回到默认值。有什么想法可能会发生吗?是否有某种刷新功能需要调用?
I have verified that the text is changing, but when I run the app and I click on the button, it changes to "Calibration" for a split second and then goes right back to its default value. Any ideas why this might be happening? Is there some sort of refresh function I need to be calling?
推荐答案
在布局其子视图时,UIButton将设置其标题文本值使用自己的标题值,以便您可以为四种状态(正常,突出显示,选择,禁用)设置最多四个不同的字符串。
When laying out its subviews, a UIButton will set its titleLabel's text value using its own title values, so that you can set up to four different strings for the four states (normal, highlighted, selected, disabled).
由于此功能,直接设置titleLabel的文本将不会持续存在,并且在布局其子视图时将被按钮重置。
Because of this feature, setting the titleLabel's text directly won't persist, and will be reset by the button when it lays out its subviews.
这是为了更改按钮状态的标题文本所需要做的。
This is what you have to do to change the title text for a button's state.
[calibrationButton setTitle:@"Calibration" forState:UIControlStateNormal];
这篇关于更改UIButton文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!