UILongPressGestureRecognizer不在UITextField上工作

我有一个LongPress手势识别器在我的viewcontroller的viewDidLoad方法中初始化如下:

longPressGesture_= [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(displayTimeFlagCallout)]; 

我在我的视图控制器中有一个tableview。 tableview有自定义单元格。 每个单元格有2个文本框。 当用户长按文本字段(startTime和endTime)时,我想调出一个自定义的popup窗口。 我不希望放大镜和复制/粘贴popup窗口作为标准行为显示在长按文本字段,因此在添加我的手势识别器之前,我正在禁用文本字段的内置长按手势识别器。 我已经将下面的代码添加到我的cellforRowAtIndexPath方法中:

 MyCustomCell_iPhone *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (cell == nil) { cell = [[MyCustomCell_iPhone alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; for (UIGestureRecognizer *recognizer in cell.startTime.gestureRecognizers) { if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]){ recognizer.enabled = NO; } } for (UIGestureRecognizer *recognizer in cell.endTime.gestureRecognizers) { if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]){ recognizer.enabled = NO; } } [cell.startTime addGestureRecognizer:longPressGesture_]; [cell.endTime addGestureRecognizer:longPressGesture_]; } 

但是,这是行不通的。 现在长时间没有事情发生。 任何想法可能是什么问题?

感谢Hetal

三个想法:

  1. 两个控件不能使用相同的长按手势识别器。 您必须为每个控件创build一个单独的手势识别器。

  2. 在文本字段中开始编辑时(假设您允许在文本字段中进行编辑),看起来手势识别器会重置。 我假设你允许编辑文本字段,如果是的话,我相信你必须设置一个委托,将禁用不是你自己的长手势识别器。 (您可以这样做,对于您的长按手势识别器,将其称为CustomLongPressGestureRecognizer ,将其用于文本字段的手势识别器,然后可以禁用不属于您自己的CustomLongPressGestureRecognizer任何UILongPressGestureRecognizer对象。

  3. 我从你的代码推断,你不是使用故事板和原型单元格,因为在这种情况下, cell不会是nil ,你的if语句永远不会调用你的代码。 但是如果你使用的是NIB,或者不使用原型单元,那么在这一点上应该没问题。