gpt4 book ai didi

ios - UIButton setTitleColor forState 效果很好,而 setTitle 却不行

转载 作者:行者123 更新时间:2023-11-29 01:23:10 25 4
gpt4 key购买 nike

从关于 SO 的问题中,我知道使用 setTitle ... forState 更改按钮标签文本是正确的:

[_capturing_button setTitle:@"take photo!" forState:UIControlStateNormal]; 

在我的代码中,用户单击时我想禁用按钮并设置文本“处理”,并在处理完成后启用按钮返回。

但是当禁用按钮时,文本消失。

-(void) initCapturingButton
{
_capturing_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
const int width = 150, height = 30;
_capturing_button.frame = CGRectMake( _main_view.frame.size.width / 2.0 - width / 2.0, 2, width, height );
_capturing_button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

[_capturing_button setTitle:@"take photo!" forState:UIControlStateNormal];
[_capturing_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//[_capturing_button setTitle:@"processing" forState:UIControlStateDisabled];
[_capturing_button setTitleColor:[UIColor purpleColor] forState:UIControlStateDisabled];

_capturing_button.enabled = YES;

[_capturing_button addTarget:self action:@selector(onCapturingButtonClick) forControlEvents:UIControlEventTouchUpInside];
[_topToolBar addSubview:_capturing_button];

点击按钮时,我只是禁用按钮:

-(void) onCapturingButtonClick
{
_capturing_button.enabled = NO;
}

处理完成后,启用返回按钮:

  -(void) processingFinished
{
_capturing_button.enabled = YES;
}

使用此代码,当应用程序处于处理模式时,按钮文本为紫色,当处于捕获模式时,颜色为白色。但是,如果我取消注释设置禁用状态标题的行,文本就会消失。

我做错了什么?

最佳答案

由于您只是在 onCapturingButtonClick 方法中禁用而不更改按钮状态,因此您可以设置 UIControlStateNormal 的标题。

-(void) onCapturingButtonClick
{
[_capturing_button setTitle:@"processing" forState:UIControlStateNormal];
_capturing_button.enabled = NO;
}

然后在你的processingFinished方法中:

  -(void) processingFinished
{
[_capturing_button setTitle:@"take photo!" forState:UIControlStateNormal];
_capturing_button.enabled = YES;
}

来自UIButton类引用文档:

UIControlStateDisabled

Disabled state of a control. This state indicates that the control is currently > disabled. You can retrieve and set this value through the enabled property.

关于ios - UIButton setTitleColor forState 效果很好,而 setTitle 却不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34355179/

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