Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss iphone keyboard

I am trying to recreate something similar to the popup keyboard used in safari.

alt text

I am able to visually reproduce it by placeing a toolbar over my view and the appropriate buttons, however i cant figure out any way to dismiss the keyboard once the user has touched the done button.

like image 265
Lounges Avatar asked Dec 23 '08 19:12

Lounges


2 Answers

There is a couple of things you need to remember. The number #1 part developers forget to set is the delegate of the textField.

If you are using the Interface Builder, you must remember that you need to set the delegate of the textField to the file Owner.

If you are not using Interface Builder then make sure you set the delegate of the textfield to self. I also include the returnType. For Example if the textField was called gameField:

gameField.delegate = self;
gameField.returnKeyType = UIReturnKeyDone;

You must also implement the UITextFieldDelegate for your ViewController.

@interface YourViewController : UIViewController <UITextFieldDelegate> 

Finally you need to use the textFieldShouldReturn method and call [textField resignFirstResponder]

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

All your textFields will use this same method so you only need to have this setup once. As long as the delegate is set for the textField, the UITextFieldDelegate is implemented for the interface, you add the textFieldShouldReturn method and call the resignFirstResponder your set.

like image 68
Niels Hansen Avatar answered Sep 29 '22 04:09

Niels Hansen


Have you tried:

[viewReceivingKeys resignFirstResponder];

where viewReceivingKeys is the UIView that is receiving the text input?

like image 42
Frank Krueger Avatar answered Sep 29 '22 06:09

Frank Krueger