Tuesday, January 26, 2010

Making the keyboard disappear

So... I said this blog was going to be about basics, right? Well, here's a really basic concept that may elude some folks. If you have a UITextField on your view, and are implemented UITextFieldDelegate in your delegate class, perhaps you have noticed that after you enter some text using the keyboard for your field, the keyboard won't disappear when you hit return? Well, here's all you have to do:


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

Since you are implementing the UITextFieldDelegate class, this method will be called on Return, your text field will give up focus, and the UIKeyboard will disappear. If you want to be more particular and a little safer in your method you can do the following:

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

No comments:

Post a Comment