我有UILabel
,显示UITextfield
的字数。它有效,但当x
小于零时,我无法更改标签的文字颜色。实际上我不知道答案在哪里,因为我可以记录/显示正确的数据,只有-1,-2..
处的颜色没有变化。
- (void)updateLabelUsingContentsOfTextField:(id)sender {
NSString *numberPlaceholder = [NSString stringWithFormat:@"%@", ((UITextField *)sender).text];
int x = 43-[numberPlaceholder length];
if (x <= 0) {
self.charNumbers.textColor = [UIColor redColor];
NSLog(@"IF DEV LOG 1 - INTEGER IS %d", x);
}
self.charNumbers.text = [NSString stringWithFormat:@"%d",x];
NSLog(@"X INTEGER IS %d", x);
if (x <= 43) {
self.charNumbers.textColor = [UIColor blackColor];
NSLog(@"IF DEV LOG 2 - INTEGER IS %d", x);
}
}
答案 0 :(得分:2)
[numberPlaceholder length]
返回无符号整数,因此不正确的比较可能是下溢的结果。尝试将[numberPlaceholder length]
转换为int,因此值的类型相同:
int x = 43 - (int)[numberPlaceholder length];