我有一个用数字键盘UIToolbar
添加inputAccessoryView
到三个的。UITextFields
在此UIToolbar
我有一个取消和完成按钮来关闭键盘,根据这篇文章:尝试将完成按钮添加到数字键盘。这对一个很有效UITextField
,可以很容易地识别。但是,我正在尝试对三个使用相同的代码UITextFields
。这是我所拥有的:
- (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];
}
我收到“无法识别的选择器发送到实例”错误,因为 cancelKeyboard 和 doneWithKeyboard 方法没有被传递给他们需要的 textField 参数。任何想法如何正确实施?
谢谢!
本