1

I have a UIPickerView combined with a UITextField that allows users to select their gender when signing up for an account. The text field has a placeholder that says "gender" to indicate what that particular text field is for. The issue I am running into is that when the user clicks on the gender bar, the picker shows the first option as being selected (in my case, "male").

I have implemented the didSelectRow delegate that updates the text fields value when a picker row is selected; however, the loading of the picker view does not trigger a selection. meaning if a user wants to select male, they have to scroll to female, then back to male to trigger the selection. Is there a way that when the picker view loads I immediately am able to catch that event and load the first available selection from my picker view?

Is there a better option for showing 2 option selections? The UISegmentedControl feels a bit out of place with the rest of the UI on that page.

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    return self.genderData.count;
}

 // The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:    (NSInteger)component
{
    return self.genderData[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.genderField.text = self.genderData[row];
}
Atul Bhatia
  • 1,633
  • 5
  • 25
  • 50
  • Did you create your custom picker view? – Adeel Ur Rehman Dec 10 '14 at 13:32
  • I'm not clear on the question. Are you asking if the picker is working properly? Because it all works fine. I click on the text field and I see the options. – Atul Bhatia Dec 10 '14 at 13:37
  • My question is that are you using UIPickerView or did you create a custom PickerView? – Adeel Ur Rehman Dec 10 '14 at 13:41
  • I am using the standard UIPickerView – Atul Bhatia Dec 10 '14 at 13:47
  • When you assign the items to UIPickerView after that you have to reload all components of UIPickerView using `[pickerView reloadAllComponents];` and after that you have to call `[pickerView selectRow:selectedRow inComponent:0 animated:NO];` The _selectedRow_ will be the row you want to select while showing the picker view. – Adeel Ur Rehman Dec 10 '14 at 13:53
  • Duplicate with http://stackoverflow.com/questions/812995/select-row-after-uipickerview-is-loaded – Nazir Dec 10 '14 at 13:55
  • This makes it so that the first row is marked as selected, however I want that text to show up in the text field as well after the text field is clicked on. How can I do that? – Atul Bhatia Dec 10 '14 at 14:13
  • In UIPickerViewDelegate `- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component` you can get the item from array using the _row_ and assign it to your textfield – Adeel Ur Rehman Dec 10 '14 at 15:48
  • yes I have that already in there. However, it doesnt get called on the first load. So when the picker view gets loaded the first item is selected, but the text field does not get set. – Atul Bhatia Dec 10 '14 at 22:55
  • Please share the code snippets so that I can get an idea of how you are doing that. – Adeel Ur Rehman Dec 11 '14 at 02:58
  • Just added it to the post – Atul Bhatia Dec 11 '14 at 03:34
  • When you call `[pickerView selectRow:selectedRow inComponent:0 animated:NO];` after that you have to call `[self pickerView:yourPickerView didSelectRow:selectedRow inComponent:0]` because `selectRow:inComponent:animated` didn't call the `didSelectRow:`. You have to explicitly call it in order to show it into the text field. – Adeel Ur Rehman Dec 11 '14 at 05:28
  • ok but the issue with that case is that when the page loads it will immediately show the first selection. I want to show the placeholder, until the field is actually clicked on and the picker is shown. As soon as the picker is shown, I want to display the initially selected value. – Atul Bhatia Dec 11 '14 at 13:16

0 Answers0