簡體   English   中英

iOS SDK-更改UIPicker字體大小的問題。 奇怪的行為

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM