Changing UIButton text(更改 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 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更改 UIButton 文本
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01