HardCode
HardCode

Reputation: 2025

Auto select UIPickerView position

I'm using one UIPickerView for two textFields. One is Country and another one is Province. The problem that I have now is when I select the Country and go back to Province UIPickerView put the same index position as country!

Here is the code

PickerView didSelectedRow event

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

selectedpickerData = [NSString stringWithFormat:@"%@", [pickerData objectAtIndex:row], row];

// Editing Country
if (activePickerField == 1) {
    txtCountry.text = selectedpickerData;
}
// Editing Province
else if (activePickerField == 2) {
    txtProvince.text = selectedpickerData;
}
}

Q1: How do I keep the index position for each fields?

Q2: How can I select the first index position of UIPickerView when the textField is empty?

Upvotes: 0

Views: 1284

Answers (1)

Amar Kulo
Amar Kulo

Reputation: 1108

UIPickerView has method

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated

which you can use to select 0 as selected index.

Then for the second question you need to verify that [UITextField text:isEqualToString:@""] and then call method above. That can happen when for example some control resign first responder.

Upvotes: 1

Related Questions