今天我有点困惑地发现,在尝试更改 UIButton
上显示的文本时,以下内容不起作用。
// 001
[[[self button] titleLabel] setText:@"Peanuts"];
我想我知道为什么,但我只是想在这里确认我的想法。 001 确实设置了 titleLabel 文本属性,但作为它的 UILabel
(UIButton
的 subview ),它不会导致 UI 重绘。这会导致 UILabel
文本属性发生内部变化,但遗憾的是 UI 中没有视觉变化。
// 002
[[self button]setTitle:@"Peanuts" forState:UIControlStateNormal];
似乎方法 setTitle:forState 是正确的方法,它有额外的开销需要状态,但确实会调用 UI 重绘,因为它是直接在 UIButton
上调用的。我的问题是,002 是执行此操作的正确方法吗,除非我做的事情完全错误,否则似乎是正确的?
最佳答案
你是对的。
[[self button]setTitle:@"Peanuts" forState:UIControlStateNormal];
是设置按钮标题的方式。这使您可以控制按钮在其所有不同控制状态下的外观。
来自文档:
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the value for UIControlStateNormal is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
关于iphone - 为 UIButton 设置 titleLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12265905/