我想通过触摸 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/