ios - 通过触摸屏幕隐藏键盘

标签 ios uiview keyboard uitouch

我想通过触摸 View 来隐藏键盘。大家都推荐用这个方法,说不用链接什么的,但是不行。

问题是我的方法没有被调用,还有什么需要做的吗?

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self view] endEditing:YES];
}

最佳答案

我在这方面遇到了麻烦,所以使用一种循环遍历所有 View 的方法,看看它们是否是 TextView 和 firstResponders。不确定 UITextView 的结构,但您可能也需要检查它,尽管它可能被覆盖。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UIView *txt in self.view.subviews){
        if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
            [txt resignFirstResponder];
        }
    }
}

关于ios - 通过触摸屏幕隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28478156/

相关文章:

iOS 7 取消交互过渡无法释放呈现的 View Controller

ios - UICollectionViewCell 没有选择项目不工作

objective-c - 以编程方式更改 iPhone 5 的 UIView 布局

ios - Xcode6-Beta3 更新后,无法使用 Swift 访问键盘信息

iphone - Objective-C:在设备上创建文本文件并轻松检索文件

ios - MapKit iOS8 中的内存泄漏

ios - 使用 UIView 作为按钮

ios - UITapGestureRecognizer 的 Action 已经在进行中,无法调用?

swift - 'resignFirstResponder()' 返回 true 并且文本字段失去焦点但键盘不会关闭

cordova - 显示/隐藏 Cordova 的软键盘