1

我在 UITableView 上有一个 UITextField,并且我使用的是数字键盘,但是我希望当用户单击除 UiTextField 之外的任何内容时将其关闭。

我见过几种解决方案,但似乎没有一个明确的答案。例如,一些关于手势的讨论,当我实现它们时,它们似乎无法使用下面的代码工作:

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

    [[self view] endEditing:TRUE];

}

如您所见,我正在尝试,但似乎没有一种方法可行。有人可以指导我吗?

谢谢

4

3 回答 3

3

您应该改用 resignFirstResponder :

[textField resignFirstResponder];
于 2012-09-01T20:54:36.980 回答
3

通常,您会希望对不应关闭键盘的区域进行点击测试;但一般来说,要求是告诉当前处于焦点的控件“退出”它的状态为“firstResponder”。它可能看起来像这样:

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

但是,您可能还需要为此考虑一个特殊的手势识别器,这样您就不必从长远来看过度分析触摸的 NSSet(将确定实际“解雇请求”之间差异的任务委托给 GestureRecognizer !”点击和“我可以滚动这个吗?”滑动。

于 2012-09-01T20:55:37.993 回答
2

使用以下代码

-(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];
}

这有助于做你想做的事

于 2012-09-01T20:56:56.637 回答