【问题标题】:UILongPressGestureRecognizer not working on UITextFieldUILongPressGestureRecognizer 在 UITextField 上不起作用
【发布时间】:2012-11-30 21:25:41
【问题描述】:

我在视图控制器的 viewDidLoad 方法中初始化了一个 LongPress 手势识别器,如下所示:

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

我的视图控制器中有一个表格视图。表格视图具有自定义单元格。每个单元格有 2 个文本字段。当用户长按文本字段(开始时间和结束时间)时,我想调出一个自定义弹出框。我不希望放大镜和复制/粘贴弹出框在长按文本字段时显示为标准行为,因此在添加我的手势识别器之前,我禁用了文本字段的内置长按手势识别器。我已将以下代码添加到我的 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_];


  }

但是,这不起作用。现在长按什么都没有。任何想法可能是什么问题?

谢谢 赫塔尔

【问题讨论】:

标签: ios uitextfield uigesturerecognizer


【解决方案1】:

三个想法:

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

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

  3. 我从您的代码中推断出您没有使用情节提要和原型单元,因为在这种情况下,cell 永远不会是 nil 并且您的 if 语句将永远不会调用您的代码。但是,如果您使用 NIB 或不使用原型单元,那么在这一点上应该没问题。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多