我有一个应用程序,顶部导航栏上有一个搜索字段。当用户点击搜索字段时,键盘出现并且背景变灰。然而,此时如果用户点击 TableView 中的单元格,则键盘消失,单元格向下钻取到下一个屏幕。 .我如何防止这种情况?我能想到的唯一方法是检查 keyboardaard 是否存在,如果存在,则关闭键盘什么也不做。还有其他解决方法吗?
我在 tableview 单元格中也有按钮,当键盘启动时点击它也会激活。所以我想我需要一种通用的方法来知道键盘是否启动并且只是关闭并且不执行任何操作。
最佳答案
实现 UITextViewDelegate 和 UITextFieldDelegate
像这样:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([theTextView isFirstResponder] && [touch view] != theTextView) {
[tableView setUserInteractionEnabled:NO];
[textField resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
- (void)textViewDidEndEditing:(UITextView *)textView {
[tableView setUserInteractionEnabled:YES];
}
关于ios - 如何防止表格 View 在键盘启动时不选择单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31867191/