为什么在我的textField清除按钮上调用UIGestureRecognizer? [英] Why is UIGestureRecognizer being called on my textField clear button?

查看:115
本文介绍了为什么在我的textField清除按钮上调用UIGestureRecognizer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITableView with a gesture recognizer added:

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

...当点击tableview以解除键盘时,一切正常。我的问题是,当我点击UITextField上的清除按钮时,我的hideKeyboard方法也会调用。非常奇怪。

... everything works fine when tapping on the tableview to dismiss the keyboard. My problem is, my hideKeyboard method also calls when tapping on the "clear" button on my UITextField. Very strange.

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;
}
 }

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

Any help would be appreciated, thanks!

推荐答案

以下内容更为一般 - 它没有与您的具体观点相关联:

The following is a little more general - it's not coupled to your specific views:

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

另外,不要忘记设置手势识别器的代理,并且将该类标记为实现UIGestureRecognizerDelegate协议。

Also, don't forget to set the delegate for the gesture recognizer, and mark the class as implementing the UIGestureRecognizerDelegate protocol.

这篇关于为什么在我的textField清除按钮上调用UIGestureRecognizer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆