简体   繁体   中英

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

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? 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. Can anyone explain this?

If component=0 check what happens in this if statement:

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. But if component=0 this block will also be executed:

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. And final theString value will be the one set in the else block.

Try this instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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