touchesBegan 没有被调用

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,但它们都没有用。

您的触摸可能被 table 视图单元吞没了。尝试将键盘关闭代码放在 table 视图委托的 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];
   }
}