今天开发练习超级猜图,但是碰到了一个奇怪的问题 困扰我一个晚上,低效的夜晚 可恨~
示例说明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:
1 -(void)answerClick:(id)answerBtn{ 2 3 UIButton *btn=(UIButton*)answerBtn; 4 5 NSString *answerValue=[btn titleForState:UIControlStateNormal]; 6 7 //NSString *answerValue=btn.titleLabel.text; 8 9 [btn setTitle:nil forState:UIControlStateNormal];//设置为空 10 11 NSLog(@"title before set nill:%@,title forstate:%@, btn.titleLabel.text:%@",answerValue,[btn titleForState:UIControlStateNormal],btn.titleLabel.text); 12 13 }
输出: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取值,切记