键盘关闭和 UITableView 触摸问题 [英] Keyboard dismiss and UITableView touch issue
问题描述
所以我有这个包含几行的 UITableView
,当点击一行时,另一个 UIViewController
被推送到特定的行细节.我的问题是我想通过在屏幕上的任何触摸(也在 UITableView
上)关闭键盘,但是当我这样做时,并单击 UITableView
行,键盘关闭,但 didSelectRow
方法不起作用.单击单元格后,它不会推送新的 ViewController
.
So I have this UITableView
that contains few rows, when clicked on a row, another UIViewController
is pushed with the specific row details. My problem is that I want to dismiss keyboard with any touch on the screen (also on the UITableView
), but when I do it, and click on the UITableView
row, the keyboard dismisses, but the didSelectRow
method is not working. It wont push the new ViewController
once clicked on the cell.
反之亦然,一旦我更改了它,您就可以单击 TableView 单元格,关闭键盘并按下另一个 ViewController,但触摸任何其他地方都不会关闭键盘.
Also the other way around, once I change it, you can click on the TableView cell, the keyboard dismiss and the other ViewController is pushed, but the touch on any other place is not dismissing the keyboard.
我该怎么办?
- (IBAction)hideKeyboard:(id)sender
{
[tfFoodsSearchQuery resignFirstResponder];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UITableView class]])
{
// we touched a button, slider, or other UIControl
return YES; // handle the touch
}
NSLog(@"Touch");
return NO; // ignore the touch
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(hideKeyboard:)];
[self.view addGestureRecognizer:tap];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)];
gestureRecognizer.delegate = self;
[self.tvFoods addGestureRecognizer:gestureRecognizer];
}
谢谢!
推荐答案
set
[gestureRecognizer setCancelsTouchesInView:NO];
而不是将其添加到 tvFoods,而是添加到整个 self.view,因此它不会影响覆盖的定义.
and instead of adding it to the tvFoods, add to entire self.view, so it won't affect overridden definitions.
另外,去掉shouldReceiveTouch委托,没必要
also, remove the shouldReceiveTouch delegate, it is not necessary
这篇关于键盘关闭和 UITableView 触摸问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!