【问题标题】:UITapGestureRecognizer on UITextField no longer works in IOS 7.1UITextField 上的 UITapGestureRecognizer 不再适用于 IOS 7.1
【发布时间】:2014-03-11 13:03:36
【问题描述】:

我将UITapGestureRecognizer 附加到UITextField 以获得类似“下拉”的效果。当UITextField 被点击时,我会呈现一个带有内容的UIPopover。这就像 7.1 之前的魅力 - 现在 UITextField 刚刚成为第一响应者,并且完全忽略了手势识别器。尝试将 delaysTouchedBegan 设置为 YES,但没有帮助。有帮助吗?

【问题讨论】:

  • 我也面临同样的问题?想知道原因。

标签: ios iphone objective-c uitapgesturerecognizer ios7.1


【解决方案1】:

为什么要用UITapGestureRecognizer,最好用UITextFieldDelegate方法

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{

   //Do what you need to do...

}

您可以将textView 包裹在UIView 中,然后在该视图中添加UITapGestureRecognizer

【讨论】:

  • 我只是将 textview 包裹在另一个视图中。但需要禁用文本字段的用户交互。
  • 当文本字段已经是第一响应者时,将不会调用textFieldShouldBeginEditing: 委托方法。通过实现@JuroSheridan 回答的手势识别器委托,它应该在所有情况下都能正常工作。
【解决方案2】:

为您的点击手势实现委托方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
   return YES;
}

然后设置 yourTapGesture.delegate = self;

【讨论】:

  • 这应该是公认的答案。使用 textViewShouldBeginEditing 有一些问题。如果您的视图控制器中有多个 UITextField,则会随机调用所有 UITextField 的 textViewShouldBeginEditing。
【解决方案3】:

实现UITextField的委托方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
      // Show popover here

      return NO;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 2016-01-21
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多