1

我的应用程序中有许多 UIPickers。我让他们设置当用户点击文本控件时,选择器出现并且用户可以选择一个值。然后该值显示在文本字段中,但我希望在 UIPicker 出现时使用初始值填充文本字段。目前,在用户实际移动选择器之前,不会填充文本字段。

代码:

-(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: activityPicker])
    {
        activity.text = [activityArray objectAtIndex:row];
    }
    else if([pickerView isEqual: intensityPicker])
    {
        intensity.text = [intensityArray objectAtIndex:row];
    }
}

问候,斯蒂芬

4

1 回答 1

0

在文本字段委托方法上打开选择器之前

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
 if(textField.tag==kINTENSITYTEXTTAG)
 {
  if(![intensity.text length])
  {
   intensity.text = [intensityArray objectAtIndex:0];
  }
 }
// SAME FOR REMAINING textFields Based on the tag to get which textfield is selected
}

快乐的编码...

于 2010-09-22T08:24:28.353 回答