更改 UIButton 文本 [英] Changing UIButton text

查看:24
本文介绍了更改 UIButton 文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在单击 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 会使用自己的标题值设置其 titleLabel 的文本值,这样您就可以为这四个设置最多四个不同的字符串状态(正常、突出显示、选中、禁用).

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 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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