我有一個UIToolbar
,我將其作爲inputAccessoryView
添加到帶有數字小鍵盤的三個UITextFields
。在這UIToolbar
我有一個取消和完成按鈕來關閉鍵盤按照這篇文章:trying to add done button to Numeric keyboard。這對於一個UITextField
很有用,可以很容易地識別。但是,我試圖使用三個UITextFields
的相同代碼。下面是我有:使用inputAccessoryView UIToolbar用於多個UITextField的
- (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方法不被傳遞他們所需要的文本框參數「無法識別的選擇發送到實例」的錯誤。任何想法如何正確實施?
謝謝!
本
這正是我一直在尋找 - 謝謝! –