UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)]; [self.tableView addGestureRecognizer:gestureRecognizer];
- (void) hideKeyboard { [textField1 resignFirstResponder]; [textField2 resignFirstResponder]; ... ... }
如果只是这样,手势会覆盖点击tableView中cell时的事件,所以要这样做:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)]; gestureRecognizer.numberOfTapsRequired = 1; gestureRecognizer.cancelsTouchesInView = NO; [self.tableView addGestureRecognizer:gestureRecognizer];