简体   繁体   English

UILongPressGestureRecognizer在UITextField上不起作用

[英]UILongPressGestureRecognizer not working on UITextField

I have a LongPress gesture recognizer initialized in the viewDidLoad method of my viewcontroller as below: 我在viewcontroller的viewDidLoad方法中初始化了LongPress手势识别器,如下所示:

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

I have a tableview in my viewcontroller. 我的viewcontroller中有一个tableview。 The tableview has custom cells. tableview具有自定义单元格。 Each cell has 2 textfields. 每个单元格都有2个文本字段。 I want to bring up a custom popover when a user long presses on the text fields (startTime and endTime). 当用户长按文本字段(startTime和endTime)时,我想显示一个自定义弹出窗口。 I do not want the magnifying glass and the Copy/Paste popover to show up on long press of textfield as the standard behavior and hence before adding my gesture recognizer, I am disabling the in-built long press gesture recognizer of the text fields. 我不希望长按文本框时将放大镜和“复制/粘贴”弹出窗口显示为标准行为,因此,在添加手势识别器之前,我将禁用文本字段的内置长按手势识别器。 I have added the following code to my cellforRowAtIndexPath method: 我已经将以下代码添加到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_];


  }

However, this is not working. 但是,这不起作用。 Nothing happens on long press now. 长按现在没有任何反应。 Any ideas what could be the issue? 任何想法可能是什么问题?

Thanks Hetal 谢谢赫塔尔

Three thoughts: 三个想法:

  1. You cannot use the same long press gesture recognizers for two controls. 您不能将相同的长按手势识别器用于两个控件。 You have to create a separate gesture recognizer for each control. 您必须为每个控件创建一个单独的手势识别器。

  2. It would appear that the gesture recognizers get reset when you start editing in text field (assuming you allow editing in the text field). 当您开始在文本字段中进行编辑时(假设您允许在文本字段中进行编辑),手势识别器似乎会重置。 I assume you allow editing of the text field, and, if so, I believe that you have to set a delegate that will disable the long gesture recognizer that is not your own. 我假设您允许编辑文本字段,并且,如果这样,我认为您必须设置一个委托来禁用不是您自己的长手势识别器。 (You can do that, for your long press gesture recognizer, subclass it as, say CustomLongPressGestureRecognizer , use that for your text field's gesture recognizers and then you can disable any UILongPressGestureRecognizer objects that are not your own CustomLongPressGestureRecognizer .) (您可以这样做,对于长按手势识别器,将其子类化为CustomLongPressGestureRecognizer ,将其用于文本字段的手势识别器,然后可以禁用不是您自己的CustomLongPressGestureRecognizer任何UILongPressGestureRecognizer对象。)

  3. I infer from your code that you're not using storyboards and prototype cells, because in that scenario, cell is never nil and your if statement would never end up invoking your code. 我从您的代码推断出您没有使用情节提要和原型单元,因为在这种情况下, cell永远不会nil并且if语句永远不会最终调用您的代码。 But if you're using NIBs or not using prototype cells, then you should be fine on this point. 但是,如果您使用的是NIB或不使用原型单元,那么在这一点上应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM