今天开发练习超级猜图,但是碰到了一个奇怪的问题 困扰我一个晚上,低效的夜晚 可恨~
示例说明1
[button setTitle:@"" forState:UIControlStateNormal];
[button setTitle:nil forState:UIControlStateNormal];
//设置失效 因为 当再次获取button的值时,却是在更早的值 (btn.titleLabel.text=在设置为空之前的值)
[view setTitle: @"" forState :UIControlStateNormal ];
[view setTitle: @"" forState :UIControlStateHighlighted ];
[view setTitle: @"" forState :UIControlStateDisabled ];
[view setTitle: @"" forState :UIControlStateSelected ];
[view setTitle: @"" forState :UIControlStateApplication ];
[view setTitle: @"" forState :UIControlStateReserved ];
//设置失效 (btn.titleLabel.text=在设置为空之前的值)
示例说明2:
-(void)answerClick:(id)answerBtn{ UIButton *btn=(UIButton*)answerBtn; NSString *answerValue=[btn titleForState:UIControlStateNormal]; //NSString *answerValue=btn.titleLabel.text; [btn setTitle:nil forState:UIControlStateNormal];//设置为空 NSLog(@"title before set nill:%@,title forstate:%@, btn.titleLabel.text:%@",answerValue,[btn titleForState:UIControlStateNormal],btn.titleLabel.text); }
输出:2014-09-03 19:59:03.606 05-1超级猜图[895:60b] title before set nill:笑,title forstate:(null), btn.titleLabel.text:笑
原因分析
示例1中 根据UIControlStateNormal设置为空,则必须通过[btn titleForState:UIControlStateNormal] 获取值,如果通过btn.titleLabel.text 会得到设置nil之前的值
因此 通过State赋值 一定要通过State取值,切记