【发布时间】:2015-12-18 11:57:29
【问题描述】:
从关于 SO 的问题中,我知道用setTitle ... forState 更改按钮标签文本是正确的:
[_capturing_button setTitle:@"take photo!" forState:UIControlStateNormal];
在我的代码中,在用户单击时我想禁用按钮并设置文本"processing",并在处理完成后启用按钮返回。
但是当禁用按钮时,文字消失了。
-(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;
}
使用此代码,当应用程序处于处理模式时,按钮文本为紫色,当处于捕获模式时,颜色为白色。但是,如果我取消注释为禁用状态设置标题的行,文本就会消失。
我做错了什么?
【问题讨论】:
-
您是否尝试过设置断点并检查按钮的状态?您能否确认已设置标题文本,即使它没有在 UI 中显示?
-
@fragilecat:是的,我试过了。状态为
UIControlStateDisabled,文本为“处理中”。 -
如果将代码行替换为 [_capturing_button setTitle:@"take photo!" 会发生什么情况forState:UIControlStateNormal];?