views:

78

answers:

1

Hello,

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

A: 

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...

Suriya
What do I declare kINTENSITYTEXTTAG as ?
Stephen
its any number that you have given as tag to your textfield
Suriya
Suriya, thanks for the response, but I didn't get the desired result. I'm adding my UIPickers as subviews. In IB I've unchecked the 'User Interaction Button', this allows me to detect where the user has tapped and hence load the appropriate UIPicker. But this means that 'textFieldDidBeginEditing' does not get called. Any thoughts ?
Stephen
okay then on the tap of that button call the above thing For example when you tap the button for intensity UIPicker at that time call the `if(![intensity.text length]) { intensity.text = [intensityArray objectAtIndex:0]; }` and so on for rest of all
Suriya
Thanks Suriya, this works on all pickers, expect the ones with numeric components. i.e I have a picker that allows the user to select the time in hh:mm:ss format (3 components). If I select the hour from the first component my UITextField looks like this: 10:(null):(null). The (null) can be removed when the user selects the appropriate mm and ss. But if the user just wants to enter 10 hours, I want the other values to be 00 instead of (null), without the user having to physically move the picker components. Any further thoughts on this one ?
Stephen