0

UITextField を含む UIView がありますが、それらをクリックすると、キーボードを非表示にすることができず、「送信」ボタンがキーボードの後ろにあります。

ここに画像の説明を入力

どうすれば隠すことができますか?

ありがとう!

4

2 に答える 2

4

Add UITextField's delegate method in your code.Don't forget to set the delegate.Whenever return key is keyboard is pressed this below method will be called. Also set its delegates:

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

EDIT : when once focused to textField and focus to other textfield, that textfield will not resign so do this as i assume from topmost u have textField1, textField2, textField3, textField4 if any more.... Add this delegate method.

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

EDIT U can use this also:

   - (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 に答える