【问题标题】:touchesBegan not getting calledtouchesBegin 没有被调用
【发布时间】:2015-06-12 00:50:45
【问题描述】:

我在static UITableVieweCells 中有几个UITextFields,当用户点击其他地方时,我试图关闭键盘。这是我的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   [self.view endEditing:YES];
    NSLog(@"touch began");
}

当我在其他地方录制时,我没有收到任何NSLogs。我做错了什么?`

然后我尝试了self.view.userInteractionEnabled = YES;self.tableView,它们都不起作用。

【问题讨论】:

  • touchesBegan:withEvent: 方法在哪里?
  • 在tableview的.m文件中

标签: ios objective-c uitableview uitextfield touchesbegan


【解决方案1】:

您的触摸可能被表格视图单元格吞噬。尝试将您的键盘关闭代码放在表视图委托的tableView:didSelectRowAtIndexPath: 中。

编辑:

也许你应该结合使用手势识别器和手势识别器:

UITapGestureRecognizer *navBarTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
[self.navigationController.navigationBar addGestureRecognizer:navBarTap];

UITapGestureRecognizer *tableViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
[self.tableView addGestureRecognizer:tableViewTap];

【讨论】:

  • 如果我这样做,那么如果用户点击导航栏,那么[self.view endEditing:YES] 将不会被调用。
【解决方案2】:

在 UIScrollview 中不调用 Touchesbegan,而 UITableview 是 uiScrollview 的子类,因此请创建 UITableview 的子类并全部实现

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

// If not dragging, send event to next responder
  if (!self.dragging){ 
    [self.nextResponder touchesBegan: touches withEvent:event]; 
  }
  else{
    [super touchesEnded: touches withEvent: event];
  }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

// If not dragging, send event to next responder
    if (!self.dragging){ 
     [self.nextResponder touchesBegan: touches withEvent:event]; 
   }
   else{
     [super touchesEnded: touches withEvent: event];
   }
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

  // If not dragging, send event to next responder
   if (!self.dragging){ 
     [self.nextResponder touchesBegan: touches withEvent:event]; 
   }
   else{
     [super touchesEnded: touches withEvent: event];
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 2019-03-31
    相关资源
    最近更新 更多