UILongPressGestureRecognizer 不适用于 UITextField [英] UILongPressGestureRecognizer not working on UITextField

查看:19
本文介绍了UILongPressGestureRecognizer 不适用于 UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a LongPress gesture recognizer initialized in the viewDidLoad method of my viewcontroller as below:

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

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

I have a tableview in my viewcontroller. The tableview has custom cells. Each cell has 2 textfields. I want to bring up a custom popover when a user long presses on the text fields (startTime and 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:

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?

谢谢赫塔尔

推荐答案

三个想法:

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

  1. You cannot use the same long press gesture recognizers for two controls. You have to create a separate gesture recognizer for each control.

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

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.)

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

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. But if you're using NIBs or not using prototype cells, then you should be fine on this point.

这篇关于UILongPressGestureRecognizer 不适用于 UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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