I found it frustrating that editable UITextFields don’t automatically dismiss the keyboard when editing is done. I think this should be the default behavior, but what do I know?
First thing you need to do is implement the UITextFieldDelegate in your UIViewController.
@interface MyViewController : UIViewController <UITextFieldDelegate> {
The next is to set the controller as the UITextField delegate (I do this in the viewDidLoad method).
- (void)viewDidLoad { [super viewDidLoad]; [myTextField setDelegate:self]; } Then, implement textFieldShouldReturn in your controller like this;
#pragma mark UITextFieldDelegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } The keyboard is the "first responder", and this causes it to go away when the "done" button is pressed (assuming you have a "done" button on your keyboard). Can anyone tell me why the numeric keypad doesn't have a done button??
Hi what happens if you have 2 text fields ? Just dismiss both ?
Yes, it will dismiss each one. that is because it refers to the text field that called it. You might want to tweak the delegate calls that move the view so that the offset (100) is based on the field position on the view.
Oops! I thought your response was to another post where I mention this keyboard thing and a way to slide the view out the way of the keyboard. Sorry for the confusion.
where is the “done” button: great question! 🙂
did you ever manage to find out about the “done” button on the numeroc keypad?
i only found this: http://stackoverflow.com/questions/584538/how-to-show-button-done-on-number-pad-on-iphone but hoping there’s a more elegant solution…?
Hitting the “Done” button causes a “did end on exit” event to be generated.
Just add the following method to your controller class:
-(IBAction)textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
}
In IB connect the “Did End on Exit” event of your text field to the File’s Owner icon and connect it to the above method.
🙂
Regarding the missing “Done” button, well, there is a simple way to manipulate the keyboard by configuring the UITextField itself:
xTextField.keyboardType = UIKeyBoardTypeDefault;
xTextField.returnKeyType = UIReturnKeyDone;
xTextFiled.delegate = self;
…
etc.
The UITextField is, basically, a subView.
Thank you! This is the most helpful one I found. ^_^
Thnx a lot!! Works great!
Thanks for your help with this!