I have a UIToolbar
that I'm adding as an inputAccessoryView
to three UITextFields
with numeric keypads. On this UIToolbar
I have a cancel and a done button to dismiss the keyboard as per this post: trying to add done button to Numeric keyboard. This works great for one UITextField
, which can be easily identified. However, I'm trying to use the same code for three UITextFields
. Here's what I have:
- (void) viewDidLoad
{
[super viewDidLoad];
// weight, numberOfDrinks, and drinkingDuration are UITextFields set up in Storyboard.
// I implemented the UITextFieldDelegate protocol in the .h file and set the UITextField's delegates to the owning ViewController in Storyboard.
self.weight.delegate = self;
self.numberOfDrinks.delegate = self;
self.drinkingDuration.delegate = self;
}
- (void) textFieldDidBeginEditing:(UITextField *)textField {
UIToolbar *doneToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
doneToolbar.barStyle = UIBarStyleBlackTranslucent;
// I can't pass the textField as a parameter into an @selector
doneToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelKeyboard:)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithKeyboard:)],
nil];
[doneToolbar sizeToFit];
textField.inputAccessoryView = doneToolbar;
}
- (void) cancelKeyboard: (UITextField *) textField {
textField.text = @"";
[textField resignFirstResponder];
}
- (void) doneWithKeyboard: (UITextField *) textField {
[textField resignFirstResponder];
}
I get an "unrecognized selector sent to instance" error because the cancelKeyboard and doneWithKeyboard methods aren't being passed the textField parameter they need. Any ideas how to implement this properly?
Thanks!
Ben