有没有办法在 UIButton 上 setTitle 一次更新所有 UIControlStates ?

发布于 2024-08-04 05:51:18 字数 277 浏览 5 评论 0原文

我有一个 UIButton,我想更新它的标题,但我不想总是为每个状态执行此操作,如下所示:

[myButton setTitle:@"Play" forState:UIControlStateNormal];
[myButton setTitle:@"Play" forState:UIControlStateHighlighted];
[myButton setTitle:@"Play" forState:UIControlStateSelected];

有更好的方法吗?

I have a UIButton, and I'd like to update its title, but I'd rather not have to always do it for each and every state like the following:

[myButton setTitle:@"Play" forState:UIControlStateNormal];
[myButton setTitle:@"Play" forState:UIControlStateHighlighted];
[myButton setTitle:@"Play" forState:UIControlStateSelected];

Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

会发光的星星闪亮亮i 2024-08-11 05:51:18

根据文档,您只需要调用:

In Objective-C:

[myButton setTitle:@"Play" forState:UIControlStateNormal];

In Swift:

myButton.setTitle("Play", for: .normal)

UIButton 文档解释了原因:

一般来说,如果没有为状态指定属性,则默认使用 UIControlStateNormal 值。如果未设置 UIControlStateNormal 的值,则该属性默认为系统值。因此,您至少应该设置正常状态的值。

也就是说,如果您只设置正常值,其他状态在设置时将引用它。

According to the documentation you should only need to call:

In Objective-C:

[myButton setTitle:@"Play" forState:UIControlStateNormal];

In Swift:

myButton.setTitle("Play", for: .normal)

The UIButton docs explain why:

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.

Namely, that if you only set the normal value the other states will refer to it when being set.

苏佲洛 2024-08-11 05:51:18

或者您可以通过以下方式设置标题:

[myButton setTitle:@"Play" forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];

or you can setTitle by :

[myButton setTitle:@"Play" forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
巨坚强 2024-08-11 05:51:18

您可以为 UIButton 创建一个类别:

@implementation UIButton (Addition)
-(void)setTitleForAllStates:(NSString *)title {
     //you can add/remove this area : UIControlStateApplication, UIControlStateDisabled, UIControlStateReserved
     [self setTitle:title forState:UIControlStateNormal];
     [self setTitle:title forState:UIControlStateSelected];
     [self setTitle:title forState:UIControlStateHighlighted];
}
@end

You can create a category for UIButton:

@implementation UIButton (Addition)
-(void)setTitleForAllStates:(NSString *)title {
     //you can add/remove this area : UIControlStateApplication, UIControlStateDisabled, UIControlStateReserved
     [self setTitle:title forState:UIControlStateNormal];
     [self setTitle:title forState:UIControlStateSelected];
     [self setTitle:title forState:UIControlStateHighlighted];
}
@end
江城子 2024-08-11 05:51:18

2019年

只是

yourButton.setTitle("Click me", for: .normal)

2019

It's just

yourButton.setTitle("Click me", for: .normal)
仅此而已 2024-08-11 05:51:18

答案:

button.setTitle("All", for: .normal)

or

button.setTitle("All", for: [])

一般来说是不正确的,因为它仅在尚未设置某些状态的标题时才有效。例如:

button.setTitle("N", for: .normal)
button.setTitle("HL", for: .highlighted)
button.setTitle("All", for: .normal)

在此代码之后按钮仍然具有标题“HL”以突出显示状态。
因此,要在一般情况下更改所有使用状态的标题,您必须循环遍历所有这些状态:(

let states: [UIControl.State] = [.normal, .highlighted, .selected, [.highlighted, .selected]]
for state in states {
  button.setTitle("All", for: state)
}

如果您使用其他状态,例如 .disabled,您也必须将它们的组合添加到循环中)

注意 : UIControl.State 是一组选项,因此设置标题时:

button.setTitle("All", for: [.selected, .highlighted])

不会为选定状态和突出显示状态设置标题“全部”,而是为选定和突出显示的组合状态设置标题“全部” 同时。

The answer:

button.setTitle("All", for: .normal)

or

button.setTitle("All", for: [])

is not correct in general, because it works only if the title for some state is not already set. For example:

button.setTitle("N", for: .normal)
button.setTitle("HL", for: .highlighted)
button.setTitle("All", for: .normal)

After this code button still have title "HL" for highlighted state.
So to change title for all used states in general case, you have to loop through all these states:

let states: [UIControl.State] = [.normal, .highlighted, .selected, [.highlighted, .selected]]
for state in states {
  button.setTitle("All", for: state)
}

(If you use other states e.g. .disabled, you have to add they combinations to loop as well)

Note: UIControl.State is a Set of options, so setting the title with:

button.setTitle("All", for: [.selected, .highlighted])

does not set title "All" for both selected and highlighted states, instead it sets the title "All" for the combined state which is selected and highlighted at the same time.

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