iphone - 为 UIButton 设置 titleLabel

标签 iphone objective-c uibutton uilabel

今天我有点困惑地发现,在尝试更改 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/

相关文章:

iphone - 适合初学者的自动释放

iphone - 如何获取 NSHTTPURLResponse 的状态描述

objective-c - uinavigationcontroller 堆栈内存管理

ios - UIButton 上的圆形渐变边框

ios - 使用 objective-c 在iOS中制作一个没有图像的圆形按钮

ios - 仅在单击按钮后显示事件指示器

iphone - 如何从 UIScrollView 窃取触摸?

iphone - 调用avcodec_encode_video时的EXC_BAD_ACCESS

ios - 尝试使用 JSONModel : Invalid type in JSON write (. 序列化后出错 ..)

ios - 在 reloadData 上禁用 UICollectionView 动画