我需要跟踪哪个文本字段是我的自定义键盘工作的 firstResponder。在下面的代码中,我严重简化了我的程序,但这里是问题的要点:
@implementation SimplePickerViewController
@synthesize pickerKeyboard;
@synthesize textView;
@synthesize textView2;
@synthesize firstResponder;
-(void)viewDidLoad{
pickerKeyboard = [[PickerKeyboardViewController alloc] initWithNibName:@"PickerKeyboard" bundle:nil];
pickerKeyboard.delegate = self;
[self.textView setInputView:pickerKeyboard.view];
[self.textView setDelegate:self];
[self.textView2 setInputView:pickerKeyboard.view];
[self.textView2 setDelegate:self];
}
-(void)hideKeyboard{
[self.firstResponder resignFirstResponder];
self.firstResponder = nil; //without this line, the code doesn't work.
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
self.firstResponder = textView;
[self.pickerKeyboard.picker reloadAllComponents];
return YES;
}
如果我删除将 firstResponder 设置为 nil 的行,代码将停止正常运行,但我不知道为什么。(没有那条线,我可以选择第一个 textView 来调出键盘,但在那之后我永远无法把键盘带回来。有什么想法吗?谢谢!