2010-09-23 66 views
1

我有一個UIPicker,其中包含3個包含數字值的組件,這允許用戶以hh:mm:ss格式設置時間。如果我從第一個組件中選擇小時,我的UITextField看起來像這樣:10:(null):(null)。當用戶選擇合適的mm和ss時,可以刪除(null)。但是,如果用戶只想輸入10小時,我希望其他值爲00而不是(空),而用戶不必物理移動選取器組件。UIPicker - 初始值問題

任何人有任何想法呢?

下面的一些代碼,這個問題與timePicker有關。

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    if([pickerView isEqual: routePicker]) 
    { 
     route.text = [[routeArray objectAtIndex:0] valueForKey:@"route_name"]; 
     return [[routeArray objectAtIndex:row] valueForKey:@"route_name"]; 
    } 
    else if([pickerView isEqual: activityPicker]) 
    { 
     activity.text = [activityArray objectAtIndex:0]; 
     return [activityArray objectAtIndex:row]; 
    } 
    else if([pickerView isEqual: intensityPicker]) 
    { 
     if ([intensityArray objectAtIndex:row][email protected]"Low") 
      numberIntensity=2; 
     else if ([intensityArray objectAtIndex:row][email protected]"Low-Medium") 
      numberIntensity=3.5; 
     else if ([intensityArray objectAtIndex:row][email protected]"Medium") 
      numberIntensity=5; 
     else if ([intensityArray objectAtIndex:row][email protected]"Medium-High") 
      numberIntensity=6.5; 
     else if ([intensityArray objectAtIndex:row][email protected]"High") 
      numberIntensity=8; 
     else numberIntensity=0; 

     intensity.text = [intensityArray objectAtIndex:0]; 
     return [intensityArray objectAtIndex:row]; 
    } 
    else if([pickerView isEqual: timePicker]) 
    { 
     switch (component) 
     { 
      case 0: 
       return [hourArray objectAtIndex:row]; 
       break; 
      case 1: 
       return [minuteArray objectAtIndex:row]; 
       break; 
      case 2: 
       return [secondArray objectAtIndex:row]; 
       break; 
      default: 
       return 0; 
       break; 
     } 
    } 
    else if([pickerView isEqual: distancePicker]) 
    { 
     switch (component) 
     { 
      case 0: 
       return [distance1Array objectAtIndex:row]; 
       break; 
      case 1: 
       return [distance2Array objectAtIndex:row]; 
       break; 
      default: 
       return 0; 
       break; 
     } 
    } 
    else 
    { 
     return 0; 
    } 
} 



-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    if([pickerView isEqual: routePicker]) 
    { 
     route.text = [[routeArray objectAtIndex:row] valueForKey:@"route_name"]; 
    } 
    else if([pickerView isEqual: timePicker]) 
    { 
     if (component == 0) 
     { 
      selectedHour = [hourArray objectAtIndex:row]; 
     } 
     else if (component == 1) 
     { 
      selectedMinute = [minuteArray objectAtIndex:row]; 
     } 
     else if (component == 2) 
     { 
      selectedSecond = [secondArray objectAtIndex:row]; 
     } 
     time.text = [NSString stringWithFormat:@"%@:%@:%@", selectedHour, selectedMinute, selectedSecond]; 
    } 
    else if([pickerView isEqual: distancePicker]) 
    { 
     if (component == 0) 
     { 
      selectedDistance1 = [distance1Array objectAtIndex:row]; 
     } 
     else if (component == 1) 
     { 
      selectedDistance2 = [minuteArray objectAtIndex:row]; 
     } 
     distance.text = [NSString stringWithFormat:@"%@.%@", selectedDistance1, selectedDistance2]; 
    } 
    else if([pickerView isEqual: activityPicker]) 
    { 
     activity.text = [activityArray objectAtIndex:row]; 
    } 
    else if([pickerView isEqual: intensityPicker]) 
    { 
     intensity.text = [intensityArray objectAtIndex:row]; 
    } 
} 

回答

1

我覺得這行:

time.text = [NSString stringWithFormat:@"%@:%@:%@", selectedHour, selectedMinute, selectedSecond]; 

可改爲:

time.text = [NSString stringWithFormat:@"%@:%@:%@", 
      (selectedHour ? selectedHour : @"00"), 
      (selectedMinute ? selectedMinute : @"00"), 
      (selectedSecond ? selectedSecond : @"00")]; 

這假定selectedHourselectedMinuteselectedSecond都是最初nil這個代碼之外。我遠離我的Mac,所以我沒有測試過這個。

或者,您可以初始化selectedHourselectedMinuteselectedSecond@"00"

+0

謝謝,我剛剛初始化selectHour,selectMinute並selectSecond to @「00」。 – Stephen 2010-09-28 09:37:41