user1256477
user1256477

Reputation: 11201

keyboard how to hide it

I have a UIView with some UITextField, but when I click at them, I cant hide the keyboard and the "send" button is behind the keyboard:

enter image description here

how can I hide it?

Thanks!

Upvotes: 0

Views: 311

Answers (2)

Ricardo
Ricardo

Reputation: 2321

I use this code, this event runs when you tap anywhere in view, and you do not depend on textFields events:

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

}

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38259

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

Upvotes: 4

Related Questions