views:

2869

answers:

2

Hi

I have a pickerview on my UIView along with a UITextField. I am letting the user select a value from the picker or enter a custom value in the text field.

When the user selects any option from the picker, the variable values are changed fine (in the pickerView:didSelectRow:inComponent:) method. But if the user does not select any value on the picker (coz they want to select the 1st option which is already selected), this method is not called and the selection is not reflected in the variable.

I tried setting the default value of the variable to the first option in the picker. But in that case, when the user edits the text field, the variable will have the text field's text. Now if the user decides to pick the first value again, the new value is not reflected in the variable.

How can I overcome this? Thanks.

Here's the relevant code

In my viewDidLoad I have this statement to set the selectText to first option of the picker by default

[self setSelectText:[myArray objectAtIndex:0]];

textFieldShouldReturn

- (BOOL)textFieldShouldReturn:(UITextField *)txtField{
    [txtField resignFirstResponder];
    [self setSelectText:txtField.text];
    return YES;
}

PickerViewDelegate's didSelectRow

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // report the selection to the UI label
    [self setSelectText:[myArray objectAtIndex:[pickerView selectedRowInComponent:0]]];
}

The problem arises when the user edits the text and then decides he after all wants to use the first value of the picker. In that case they will dismiss the keyboard and click on the 1st option of the picker (which is already selected). This action does not call didSelectRow and so the value of selectText is not changed.

In order for the user to be able to change the value of selectText, they first have to select some other value from the picker and then select the 1st row.

A: 

Can you programmatically select a row right after you instantiate the UIPickerView?

int firstRow = 0;
int firstOptionIndex = 0;
[myPickerView selectRow:firstRow inComponent:firstOptionIndex animated:NO];

That last line will trigger your pickerView:didSelectRow:inComponent delegate method.

In other words, that should set the value of the variable, wherever you are storing it (NSArray? not clear).

Alex Reynolds
That would do. But suppose the user decides to enter a custom value in the text field. And then decides that he after all wants to use the 1st value in the picker. So now selectText has the value which was entered in the textfield. And the user just clicks on the 1st row thinking he'll override the value entered in the textfield. But that wouldn't happen becoz the program would not know the user tapped on the 1st option.
lostInTransit
That touching the 1st row of a picker view does not generate the selection event for you may simply be a "shortcoming" with UIPickerView's design as you need it to work, but it's not necessarily a problem with UITextField itself. You might consider filing a feature enhancement to UIPickerView in http://bugreporter.apple.com.
Alex Reynolds
+2  A: 

Instead of "pushing" the value of the picker each time you make a selection with this code:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

you should "pull it" when the user clicks the button (or other event you are using) using something similar to:

- (void)myAction:(id)sender
{

    NSInteger row = [myPicker selectedRowInComponent:0];
    selectedText = [myArray objectAtIndex:0];


}

For clearing the text: The UITextField class provides a built-in button for clearing the current text. So if the user does not want that text he can discard it.

myTextField.clearButtonMode = UITextFieldViewModeAlways;

So in the script, will you check if the TextField has a value, if it does have a value u use the TextField, if it doesn't have a value, you pull the information from the Picker.

Benjamin Ortuzar
Thanks. But the problem here is that the user has an option of specifying a custom value in a text field. So there is a possibility the user enters some text. Now if I do what you've suggested, any value the user enters will be disregarded.
lostInTransit
The UITextField class also provides a built-in button for clearing the current text. So if the user does not want that text he can discard it. myTextField.clearButtonMode = UITextFieldViewModeAlways;
Benjamin Ortuzar
I updated the answer.
Benjamin Ortuzar