Search code examples
iosobjective-cuitableviewgesturetouches

Init touch by UITapGestureRecognizer's -locationInView


I use UITapGestureRecognizer on a tableView for -endEditing after -textFieldDidBeginEditing. First I want to make -endEditing and then perform this touch on tableView's element. How can I do it right way?

#pragma mark - Text Field Delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(finishEditing:)];
    [self.tableView addGestureRecognizer:tapRecognizer];
}

- (void)finishEditing:(UITapGestureRecognizer *)tapRecognizer {
    [self.view endEditing:YES];

}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self saveName];
}

Solution

  • Setting cancelsTouchesInView to false will pass the touches through to the view.

    When this property is true (the default) and the receiver recognizes its gesture, the touches of that gesture that are pending are not delivered to the view and previously delivered touches are cancelled through a touchesCancelled:withEvent: message sent to the view. If a gesture recognizer doesn’t recognize its gesture or if the value of this property is false, the view receives all touches in the multi-touch sequence.

    See the reference here