为什么无法通过点语法更改 UIButton 文本?
是否可以通过点语法更改 UIButton 标题标签文本?如果我通过点语法执行此操作,文本不会更改:
self.myButton.titleLabel.text = [NSString stringWithString:@"Nop"];
但是这个会更改:
[self.myButton setTitle: @"Yep" forState: UIControlStateNormal];
文本更改适用于纯 UILabels,UIButton 是一种特殊情况还是其他情况?
Is it possible to change UIButton title label text via dot syntax? The text is not changing if I do it via dot syntax:
self.myButton.titleLabel.text = [NSString stringWithString:@"Nop"];
But this one does:
[self.myButton setTitle: @"Yep" forState: UIControlStateNormal];
Text change works for pure UILabels, is UIButton a special case or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为 UIButton 的文本可以更改为几种不同的状态(正常、突出显示等)。苹果只是选择需要显式代码来设置文本。如果您确实愿意,您可以创建一个类别来实现此目的。
This is because the text can be changed for a UIButton for several different states (Normal, Highlighted, etc). Apple simply chose to require explicit code to set the text. You could probably create a category to implement this, if you really wanted.
UIButton 可以为任何 UIControlState 重新配置其标题(请参见此处:UIControlState)。
由于使用点语法不会设置任何特定状态的标题,因此 UIButton 可能会重新配置 UIControlStateNormal 的标题,该标题设置为空。
UIButton's can reconfigure their titles for any of the UIControlState's (see here: UIControlState).
Since using the dot syntax doesn't set the title for any specific state, the UIButton is likely re-configuring the title for UIControlStateNormal, which is set to nothing.
试试
这里的作品
[]的
Try
here works
[]'s