Ios 在UiTableView上取消UITextField的数字键盘的最优雅方式

Ios 在UiTableView上取消UITextField的数字键盘的最优雅方式,ios,uitableview,Ios,Uitableview,我在UITableView上有一个UITextField,我使用的是数字键盘,但我希望当用户单击UITextField以外的任何内容时,它都被取消 我看到了几种解决方案,但似乎没有一个明确的答案。例如,一些人谈论手势,当我使用下面的代码实现它们时,它们似乎不起作用: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[self view] endEditing:TRUE]; } 正如你所看到的,我

我在UITableView上有一个UITextField,我使用的是数字键盘,但我希望当用户单击UITextField以外的任何内容时,它都被取消

我看到了几种解决方案,但似乎没有一个明确的答案。例如,一些人谈论手势,当我使用下面的代码实现它们时,它们似乎不起作用:

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

    [[self view] endEditing:TRUE];

}
正如你所看到的,我正在努力,但似乎没有一种方法可行。有人能给我引路吗


谢谢

您应该改用resignFirstResponder:

[textField resignFirstResponder];

您应该改用resignFirstResponder:

[textField resignFirstResponder];

通常,你会想在不应关闭键盘的区域点击测试触摸;但一般来说,要求是告诉目前处于焦点的控制部门“辞职”,它是“第一反应者”。可能是这样的:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 [self.userInput resignFirstResponder];
}

但是,您可能还需要考虑一个特殊的手势识别器,这样您就不必在长期中过度分析NSSET触控(委托给StururreCoGngisher)确定实际的“解雇请求”TAP和“我能滚动这个”之间的区别的任务吗?滑动。

通常,您会希望在不应关闭键盘的区域点击测试触摸;但一般来说,要求是告诉当前处于焦点的控件“退出”,它的状态为“第一响应者”。它可能如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 [self.userInput resignFirstResponder];
}

但是,您可能还需要考虑一个特殊的手势识别器,这样您就不必在长期中过度分析NSSET(委托给StururReCGNGNER)确定实际的“解雇请求”TAP和“我能滚动这个吗?”Sppe之间的差异的任务。< /P> < P>使用下面的代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  UITouch *touch = [[event allTouches] anyObject];
  // Note the '!':
  if(![[touch view] class] isKindOfClass [UITableViewController class]]){
    // It's not a bubble they touched, dismiss the keyboard:
    [self.view endEditing:YES];
  }
  [super touchesBegan:touches withEvent:event];
}
否则

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  UITouch *touch = [[event allTouches] anyObject];
  // Note the '!':
  if(![[touch view] class] isKindOfClass [UITableViewController class]]){
    // It's not a bubble they touched, dismiss the keyboard:
    [textField resignFirstResponder];

  }
  [super touchesBegan:touches withEvent:event];
}
这有助于执行您想要的操作

使用以下代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  UITouch *touch = [[event allTouches] anyObject];
  // Note the '!':
  if(![[touch view] class] isKindOfClass [UITableViewController class]]){
    // It's not a bubble they touched, dismiss the keyboard:
    [self.view endEditing:YES];
  }
  [super touchesBegan:touches withEvent:event];
}
否则

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  UITouch *touch = [[event allTouches] anyObject];
  // Note the '!':
  if(![[touch view] class] isKindOfClass [UITableViewController class]]){
    // It's not a bubble they touched, dismiss the keyboard:
    [textField resignFirstResponder];

  }
  [super touchesBegan:touches withEvent:event];
}

这有助于实现您想要的功能

诀窍是将
resignFirstResponder
发送到当前为第一响应者的控件(即您的UITextField)。诀窍是将
resignFirstResponder
发送到当前为第一响应者的控件(即您的UITextField)。