【问题标题】:textFieldShouldEndEditing doesn't get called when using UITapGestureRecognizer使用 UITapGestureRecognizer 时不会调用 textFieldShouldEndEditing
【发布时间】:2013-04-08 09:07:52
【问题描述】:

在我的 ViewController 中,我得到了 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 但这永远不会被调用。我搜索了为什么没有调用它,发现我使用了这个:

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];

在点击背景时关闭键盘。知道如何解决这个问题,即背景上的dismisskeyboard点击和didSelectRowAtIndexPath都可以工作吗?

【问题讨论】:

  • 您的实际要求是什么?

标签: iphone ios objective-c


【解决方案1】:

试试这样对你有帮助,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if(![[touch view] isKindOfClass:[UITextField class]]){

//resign your textfield here
    }

}

【讨论】:

  • 每当我设置 UITapGestureRecognizer 时,didSelectRowAtIndexPath 都不会被调用
【解决方案2】:

didSelectRowAtIndexPath 永远不会被调用,因为您可能没有实现 UITableViewDelegate

您应该只使用它与您的控件一起退出响应者

 [yourTextField resignFirstResponder];

【讨论】:

  • 我确实实现了这个。它是导致问题的 TapGestureRecognizer,当我删除该部分时它可以工作。
  • 如果你的 viewContoller 中有 textField ,当你在方法中点击然后 resignFirstResponderfrom 你的 textField
  • 我在 "dismisskeyboard" 方法中辞去FirstResponder
【解决方案3】:

如果您点击此视图。您必须覆盖您的委托方法shouldReceiveTouch 才能返回NO。如果触摸发生在tableView

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    // Disallow recognition of tap gestures in the segmented control.
    if ([touch.view isDescendantOfView:self.tblView]) {
        return NO;
    }
    return YES;
}

现在会触发两个委托方法。Note:self.tblView 是一个 tableView

【讨论】:

    【解决方案4】:
    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
    return TRUE;
    

    好的,看看你如何处理那个....然后如果它有效,请阅读文档以获得全面的解释,以便您获得清晰的理解。

    【讨论】:

      【解决方案5】:
      UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                     initWithTarget:self
                                     action:@selector(dismissKeyboard:)];
      
      [tbl addGestureRecognizer:tap];
      
      -(void)dismissKeyboard:(UITapGestureRecognizer *)gestureRecognizer
      {
          CGPoint p = [gestureRecognizer locationInView:tbl];
      
          NSIndexPath *indexPath = [tbl indexPathForRowAtPoint:p];
          if (indexPath == nil)
              NSLog(@"Resign");
          else
              NSLog(@"Did select Row, %i",indexPath.row);// Do your stuff as you are doing in didSelectRow
      }
      

      实现这个

      【讨论】:

      • 这个方法调用了吗?
      • 它确实被调用了,问题是 didSelectRowAtIndexPath 没有被调用
      • 移除那个点击手势并放置它。看看我编辑的答案'
      • 我这样做了,但如果我点击背景,键盘不会关闭
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多