为什么在我的textField清除button上调用UIGestureRecognizer?

我有一个添加了手势识别器的UITableView:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)]; [myTableView addGestureRecognizer:gestureRecognizer]; gestureRecognizer.cancelsTouchesInView = NO; 

…一切工作正常时,在桌面上点击键盘解雇。 我的问题是,我的hideKeyboard方法也调用时点击我的UITextField上的“清除”button。 很奇怪。

 commentTextField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 310, 35)]; commentTextField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter; commentTextField.borderStyle = UITextBorderStyleRoundedRect; commentTextField.textColor = [UIColor blackColor]; //text color commentTextField.font = [UIFont fontWithName:@"Helvetica" size:14.0]; //font size commentTextField.placeholder = @"Enter a comment..."; //place holder commentTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support commentTextField.keyboardType = UIKeyboardTypeDefault; // type of the keyboard commentTextField.returnKeyType = UIReturnKeySend; // type of the return key commentTextField.clearButtonMode = UITextFieldViewModeAlways; // has a clear 'x' button to the right commentTextField.delegate = self; [commentTextField setHidden:NO]; [commentTextField setEnabled:YES]; [commentTextField setDelegate: self]; hide keyboard method: - (void) hideKeyboard{ if(keyboard){ [commentTextField resignFirstResponder]; [UIView animateWithDuration:.3 delay:.0 options:UIViewAnimationCurveEaseInOut animations:^{ // start animation block [myTableView setFrame:CGRectMake(0, myTableView.frame.origin.y + 216, myTableView.frame.size.width, myTableView.frame.size.height)]; } completion:^(BOOL finished){ }]; keyboard = 0; } } 

任何帮助将不胜感激,谢谢!

以下是更一般的 – 这不是你的具体观点:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([touch.view isKindOfClass:[UITextField class]] || [touch.view isKindOfClass:[UIButton class]]) { return NO; } return YES; } 

此外,不要忘记设置手势识别器的委托,并将该类标记为实现UIGestureRecognizerDelegate协议。

我遇到过同样的问题。 在我看来,我也实行了以下方法:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ((touch.view == self.textField) && (gestureRecognizer == self.tapGestureRecognizer)) { return NO; } return YES; } 

但它仍然没有工作。 所以我在方法中设置了一个中断,并且看到当我单击该字段时,touch.view将被设置为它,但是当我单击清除button时,它将作为UIButton *进入。 那时很明显,发生了什么事情以及如何解决这个问题。 以下解决了这个问题。

 if((touch.view == self.textField || [self.textField.subviews containsObject:touch.view]) && (gestureRecognizer == self.tapGestureRecognizer)) { return NO; } 

我的问题与Ezmodius的方法是,他依赖于一个名为“textField”属性和我的UIViewController是一个控制器,从我所有的其他UIViewControllersinheritance,所以我需要实现一个更通用的方法:每当有任何UIViewController中的文本字段需要清除,我实现了以下(在同一个gestureRecognizer方法中:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([touch.view isKindOfClass:[UITextField class]] || ([touch.view isKindOfClass:[UIButton class]] && [touch.view.superview isKindOfClass:[UITextField class]])) { return NO; } return YES; } 

所以基本上是检查它是否是一个文本字段或超级视图是一个文本字段的button(在这种情况下,文本字段内的清除button),它对我来说就像一个魅力。 我在我的基础UIViewController类中实现了这一点,它适用于发生这种情况的每一个页面。