您好,我在使用多个 if 语句时遇到问题。这是我的代码:
if ([itemOnSpecial caseInsensitiveCompare: @"yes"] == NSOrderedSame) {
UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
specialLabel.text = specialPrice;
[specialLabel setHidden:NO];
}
//This statement is completely skipped
if ([isOnBulkSpecial caseInsensitiveCompare:@"yes"] == NSOrderedSame) {
UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
specialLabel.text = bulkSpecialPrice;
[specialLabel setHidden:NO];
}else{
UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
[specialLabel setHidden:YES];
}
仅考虑第二个 if 语句。第一个 if 语句似乎被完全忽略了。
最佳答案
如果您将代码更改为:
if ([itemOnSpecial caseInsensitiveCompare: @"yes"] == NSOrderedSame) {
UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
specialLabel.text = specialPrice;
[specialLabel setHidden:NO];
}
else
{
if ([isOnBulkSpecial caseInsensitiveCompare:@"yes"] == NSOrderedSame) {
UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
specialLabel.text = bulkSpecialPrice;
[specialLabel setHidden:NO];
}else{
UILabel *specialLabel = (UILabel*) [cell viewWithTag:5];
[specialLabel setHidden:YES];
}
}
那么只有当第一个 if 语句没有通过时才会调用第二个 if 语句。这样 specialLabel.text
属性不会更改两次,并且该值不会在第二个 if 中被覆盖
关于ios - 我有两个 if 语句,第一个被跳过,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18458436/