关于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;
}
使用此代码,当app处于处理模式时,按钮文本为紫色,当处于捕获模式时,颜色为白色。但是,如果我取消注释为禁用状态设置标题的行,文本消息。
我做错了什么?
答案 0 :(得分:1)
由于您只是禁用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
控件的禁用状态。该状态表示该控件当前>禁用。您可以通过enabled属性检索和设置此值。