为多个UITextField使用inputAccessoryView UIToolbar

时间:2013-06-12 08:27:08

标签: iphone objective-c selector

我有一个UIToolbar,我将inputAccessoryView作为UITextFields添加到带有数字键盘的三个UIToolbar。在这个UITextField上,我有一个取消和完成按钮,根据这篇文章解除键盘:trying to add done button to Numeric keyboard。这适用于一个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]; } 使用相同的代码。这就是我所拥有的:

{{1}}

我收到“无法识别的选择器发送到实例”错误,因为cancelKeyboard和doneWithKeyboard方法没有传递他们需要的textField参数。任何想法如何正确实现?

谢谢!

2 个答案:

答案 0 :(得分:1)

在.h文件中使用globle对象     UITextField * SelectedTextField;  //在开始编辑方法定义     SelectedTextField = textField; 然后在你的方法上使用它来辞职或展示。

这会对你有帮助。

答案 1 :(得分:1)

如果您只想关闭当前视图控制器中的键盘,可以调用视图控制器的view属性的endEditing方法:

[self.view endEditing];