gpt4 book ai didi

iOS SDK : Difference between UIButton setTitleForState and UIButton titleLabel. 文本

转载 作者:行者123 更新时间:2023-11-28 18:29:08 25 4
gpt4 key购买 nike

我有一个自定义 UIView 的问题,我有一个 UIButton subview ,我想根据这样的一些条件在初始化时设置按钮的文本:

- (void)awakeFromNib {
[super awakeFromNib];
//check for some conditions
self.testButton.titleLabel.text=@"Some Title";
}

没有任何反应,按钮的文本与 nib 文件中定义的相同,但是如果我将实现更改为:

- (void)awakeFromNib {
[super awakeFromNib];
//check for some conditions
[self.testButton setTitle:@"Some Title" forState:UIControlStateNormal];
}

它按预期工作。

有人可以向我解释这两种方法之间的区别吗?什么时候使用它们?

编辑:

建议的答案不能解释我的情况,我测试了从另一个按钮的操作更改按钮的文本,如下所示:

- (IBAction)otherButtonClicked:(id)sender {
self.testButton.titleLabel.text=@"Some Title";
}

并且按钮的文本发生了变化。我只想了解这种行为。

最佳答案

正确答案是

titleLabel

Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes.

The titleLabel property returns a value even if the button has notbeen displayed yet. The value of the property is nil for systembuttons.

setTitle

Use this method to set the title for the button. The title you specifyderives its formatting from the button’s associated label object. Ifyou set both a title and an attributed title for the button, thebutton prefers the use of the attributed title over this one.

At a minimum, you should set the value for the normal state. If atitle is not specified for a state, the default behavior is to use thetitle associated with the UIControlStateNormal state. If the value forUIControlStateNormal is not set, then the property defaults to asystem value.

关于iOS SDK : Difference between UIButton setTitleForState and UIButton titleLabel. 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38746335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com