我有一个 自定义 UIView
,它由 UIViewController
呈现。这个 View Controller 可以呈现几个这样的 View 之一。其中一个 View 有一个 UITextView
。当开始在 UITextView
中输入文本时,会显示键盘。但是,我想在用户点击 TextView 外的任何地方时关闭键盘。由于 UIViewController 可以根据特定条件呈现多个 UIViews
,我将 UITextView
委托(delegate)方法等封装在 custom UIView
中,我这样做了在 自定义 UIView
中使用通知,例如
- (void)configureView
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name:
UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name:
UIKeyboardWillHideNotification object:nil];
self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(didTapAnywhere:)];
}
-(void) keyboardWillShow:(NSNotification *) note {
[self addGestureRecognizer:self.tapRecognizer];
}
-(void) keyboardWillHide:(NSNotification *) note
{
[self removeGestureRecognizer:self.tapRecognizer];
}
-(void)didTapAnywhere: (UITapGestureRecognizer*) recognizer {
[self.detailsTextView resignFirstResponder];
}
但是,当 didTapAnywhere 被调用时,即使 resignfirstresponder 连接到位,键盘也不会消失。仅供引用,UITextView
的委托(delegate)是自定义 UIView
。
请帮助如何做到这一点
最佳答案
尝试
[self.view endEditing:YES];
而不是让第一响应者辞职。这应该可以解决问题。 奇怪的是为什么标准方法不起作用...
关于ios - UItextView 在 textview 外点击时关闭键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10611831/