0

我有一个带有一些 UITextField 的 UIView,但是当我点击它们时,我无法隐藏键盘,并且“发送”按钮位于键盘后面:

在此处输入图像描述

我该如何隐藏它?

谢谢!

4

2 回答 2

4

在您的代码中添加 UITextField 的委托方法。不要忘记设置委托。只要按下键盘返回键,就会调用下面的方法。还要设置它的代表:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField
 {
     [textField resignFirstResponder];
     return YES;
 }

编辑:一旦专注于 textField 并专注于其他文本字段,该文本字段将不会辞职,所以这样做,因为我从最顶层假设你有 textField1、textField2、textField3、textField4,如果有的话......添加这个委托方法。

  - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  {
     if(textField == textField1)  //motive resign other textFields
     {
         [textField2 resignFirstResponder];
         [textField3 resignFirstResponder];
         [textField4 resignFirstResponder];
     }
     else if(textField == textField2)  //motive resign other textFields
     {
         [textField1 resignFirstResponder];
         [textField3 resignFirstResponder];
         [textField4 resignFirstResponder];
     }
     else if(textField == textField3)  //motive resign other textFields
     {
         [textField1 resignFirstResponder];
         [textField2 resignFirstResponder];
         [textField4 resignFirstResponder];
     }
     else if(textField == textField4)  //motive resign other textFields
     {
         [textField1 resignFirstResponder];
         [textField2 resignFirstResponder];
         [textField3 resignFirstResponder];
     }
     return YES;
  }

编辑你也可以使用它:

   - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  {
     [self.view endEditing:YES];
     return YES;
  }
于 2012-09-14T06:54:36.300 回答
0

我使用此代码,当您点击视图中的任何位置时,此事件就会运行,并且您不依赖于 textFields 事件:

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

UITouch *touch = [[event allTouches] anyObject];

if ([textField1 isFirstResponder] && [touch view] != textField1) {
    [textField1 resignFirstResponder];
}

if ([textField2 isFirstResponder] && [touch view] != textField2) {
    [textField2 resignFirstResponder];
}

...

[super touchesBegan:touches withEvent:event];

}

于 2012-12-21T08:33:20.167 回答