[英]iOS SDK - issue changing UIPicker font size. strange behavior
我不明白为什么此代码不起作用
我正在使用此委托方法来调整字体大小(我不打扰这,因为它不相关)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
这是一个多选择器。 我有三个组成部分。 当我使用条件语句使用else运行代码时,第0部分与第2部分匹配。我无法解释这一点
NSLog(@"%i", component); // only prints, 0, 1, 2
NSString *theString = @"";
if(component == 0){
theString = [_phosType objectAtIndex:row];
}
if(component == 1){
theString = [_quantity objectAtIndex:row];
} else { // THIS CAUSES PROBLEMS.
theString = [_units objectAtIndex:row];
}
pickerViewLabel.text = theString;
这个工作..什么给!
NSLog(@"%i", component); // only prints, 0, 1, 2
NSString *theString = @"";
if(component == 0){
theString = [_phosType objectAtIndex:row];
}
if(component == 1){
theString = [_quantity objectAtIndex:row];
}
if(component == 2){ // THIS WORKS! BUT WHY?!
theString = [_units objectAtIndex:row];
}
pickerViewLabel.text = theString;
为什么我需要明确询问组件是否为2? 我可以看到当我的NSLog组件永远不等于0 1或2时,我在代码的其他所有地方都使用了“ else”,并且遇到了问题。 谁能解释一下?
如果component=0
请检查此if语句中发生了什么:
if(component == 1){
theString = [_quantity objectAtIndex:row];
}
else {
theString = [_units objectAtIndex:row];
}
您可以看到else块将被执行,因为它将评估if(component == 1)
为false,而else块将被执行。 但是,如果component=0
该块也将被执行:
if(component == 0){
theString = [_phosType objectAtIndex:row];
}
因此,当component=0
theString
将被设置两次:在第一个if块和else块。 最后的theString
值将是else块中的第一个值。
尝试以下方法:
if(component == 0){
theString = [_phosType objectAtIndex:row];
}
else if(component == 1){
theString = [_quantity objectAtIndex:row];
}
else {
theString = [_units objectAtIndex:row];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.