ios – 为什么在我的textField清除按钮上调用UIGestureRecognizer?

我有一个添加了手势识别器的UITableView:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[myTableView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;

…点击桌面视图关闭键盘,一切都正常.我的问题是,当我的UITextField上点击“清除”按钮时,我的hideKeyboard方法也会调用.很奇怪.

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协议.

以上是来客网为你收集整理的ios – 为什么在我的textField清除按钮上调用UIGestureRecognizer?全部内容,希望文章能够帮你解决ios – 为什么在我的textField清除按钮上调用UIGestureRecognizer?所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。