I have a UItable with a UItextbox in it. When the user taps the textbox, the UIPickerView is presented and allows the user to make a choice. There is a toolbar attached to the UIPickerView with a done button. I can not get the done button to resign the UIPickerView.
I have multiple UIPickerViews as this is a requirement. So I placed my toolbar in its own method to reuse it.
The picker delegate methods all work and populate the correct text and update the textbox. I just can't get the picker to dismiss.
in my viewDidLoad
self.toolbar = [[UIToolbar alloc] init];
self.pickerColor = [[UIPickerView alloc] init];
[self.pickerColor setDataSource:self];
[self.pickerColor setDelegate:self];
self.pickerColor.showsSelectionIndicator = YES;
self.txt_color.delegate = self;
self.txt_color.inputView = self.pickerColor;
self.txt_color.inputAccessoryView = [self returnPickerToolbar];
There are other pickers (pickerShape, pickerNumber) all with corresponding textboxes.
The Toolbar
- (UIToolbar*) returnPickerToolbar {
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:self
action:@selector(doneData:)];
[self.toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
return self.toolbar;
}
The done button
- (void) doneData : (id) sender {
UIPickerView * picker = (UIPickerView*)sender;
[picker resignFirstResponder];
}
You need to resignResponder
of UITextField
not the UIPickerView
.
Your picker is the inputView of that textfield, so resigning responder of that textfield will automatically dismiss the picker.
- (void)doneData:(id) sender
{
[self.txt_color resignFirstResponder];
// Also you can use [self.view endEditing];
}