ios - UItextView 在 textview 外点击时关闭键盘

标签 ios uiview uitextview uikeyboard

我有一个 自定义 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/

相关文章:

ios - 在 Swift 中编辑 TableViewCell 的内容列表

ios - 构建失败 : Could not find an option named "track-widget-creation"

iphone - 将 View 移动到新的 super View 是否会自动将其从旧的 super View 中释放?

ios - UIView 应该捕获点击事件但传递移动事件

iOS - UITextView + NSUndoManager

ios - 超出父 View 时隐藏标签/按钮

ios - 添加贴纸时来自 CgImageRef 的 UIImage

multithreading - 从 UIView 制作 UIImage 但不在主线程中

iOS 7 UITextView 链接检测在 UITableView 中崩溃

ios - UITextView 的 UIGestureRecognizer 阻止键盘在点击时出现