2013-08-17 21 views
0

我不明白爲什麼這個代碼不工作iOS SDK - 更改UIPicker字體大小的問題。奇怪的行爲

我使用這個委託方法來調整字體(我不打擾顯示,由於其不相關)

- (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以外的任何東西。我在代碼中的其他地方使用'其他',並有問題。任何人都可以解釋嗎?

+0

這是當你編寫過深夜發生了什麼 – hamobi

回答

0

如果component=0檢查什麼在這個if語句情況:

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

你可以看到,別的塊將被執行,因爲它會評估if(component == 1)爲假,else塊將被執行。 但如果component=0這一塊也將被執行:

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

所以,當component=0theString將設置兩次:第一,如果塊和else塊。最後的theString值將是在else塊中設置的值。

試試這個:

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