简体   繁体   中英

Setting initial value in the UIPicker

I have a number of UIPickers in my app. I have them setup that when the user taps on a text control, the picker appears and the user can select a value. That value is then displayed in the text field, but I want the text field to be populated with the initial value when UIPicker appears. At the moment the text field is not populated until the user physically moves the picker.

Code:

-(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];
    }
}

Regards, Stephen

on the textfield delegate method Before opening the picker

- (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
}

hAPPY cODING...

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