[英]touchesBegan not getting called
我在static UITableVieweCells
有一些UITextFields
,并且我试图在用户点击其他位置时关闭键盘。 这是我的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
NSLog(@"touch began");
}
当我在其他地方进行录音时,没有得到任何NSLogs
。 我做错了什么?
然后,我尝试了self.view.userInteractionEnabled = YES;
和self.tableView
,但它们都没有起作用。
表格视图单元可能会吞噬您的触摸。 尝试将键盘关闭代码放在表视图委托的tableView:didSelectRowAtIndexPath:
。
编辑:
也许那时候您应该结合使用这种识别器和手势识别器:
UITapGestureRecognizer *navBarTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
[self.navigationController.navigationBar addGestureRecognizer:navBarTap];
UITapGestureRecognizer *tableViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
[self.tableView addGestureRecognizer:tableViewTap];
在UIScrollview中未调用Touchesbegan,并且UITableview是uiScrollview的子类,因此请使UITableview的子类实现所有
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesBegan: touches withEvent:event];
}
else{
[super touchesEnded: touches withEvent: event];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesBegan: touches withEvent:event];
}
else{
[super touchesEnded: touches withEvent: event];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesBegan: touches withEvent:event];
}
else{
[super touchesEnded: touches withEvent: event];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.