为什么无法通过点语法更改 UIButton 文本?

发布于 2024-11-28 20:55:48 字数 308 浏览 2 评论 0原文

是否可以通过点语法更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终止放荡 2024-12-05 20:55:49

这是因为 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.

不必在意 2024-12-05 20:55:49

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.

贱贱哒 2024-12-05 20:55:49

试试

myButton.titleLabel.text=@"foo";

这里的作品

[]的

Try

myButton.titleLabel.text=@"foo";

here works

[]'s

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文