简体   繁体   English

iOS SDK-更改UIPicker字体大小的问题。 奇怪的行为

[英]iOS SDK - issue changing UIPicker font size. strange behavior

I can not understand why this code does not work 我不明白为什么此代码不起作用

I am using this delegate method to resize the font (i dont bother showing that because its not relevant) 我正在使用此委托方法来调整字体大小(我不打扰这,因为它不相关)

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

this is a multi picker. 这是一个多选择器。 i have three components. 我有三个组成部分。 When I run the code with a conditional statement using else, it makes section 0 match section 2. I can't explain this 当我使用条件语句使用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;

this one works.. what gives! 这个工作..什么给!

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;

why do i need to explicitly ask if component is 2? 为什么我需要明确询问组件是否为2? i can see when I NSLog component that it never equals anything other than 0 1 or 2. I'm using 'else' everywhere else in the code and having the problem. 我可以看到当我的NSLog组件永远不等于0 1或2时,我在代码的其他所有地方都使用了“ else”,并且遇到了问题。 Can anyone explain this? 谁能解释一下?

If component=0 check what happens in this if statement: 如果component=0请检查此if语句中发生了什么:

if(component == 1){
    theString = [_quantity objectAtIndex:row];
}
else {                                       
   theString = [_units objectAtIndex:row];
}

You can see that else block will be executed because it will evaluate if(component == 1) as false and else block will be executed. 您可以看到else块将被执行,因为它将评估if(component == 1)为false,而else块将被执行。 But if component=0 this block will also be executed: 但是,如果component=0该块也将被执行:

if(component == 0){
    theString = [_phosType objectAtIndex:row];
}

So, when component=0 theString will be set two times: in the first if block and in the else block. 因此,当component=0 theString将被设置两次:在第一个if块和else块。 And final theString value will be the one set in the else block. 最后的theString值将是else块中的第一个值。

Try this instead: 尝试以下方法:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM