IPhone Keyboard, Finish and ResignFirstResponder

This is probably a dumb question, but I can not find the answer in the documents. Did the keyboard disappear when you click Finish on the pop-up keyboard? I see a lot of code on the Internet like this:

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

When I click Finish, the keyboard pops down and UITextField resigns from the first responder.

I assume that clicking Finish did not use to invoke from UITextField to resignFirstResponder , but this behavior changed after a while.

I am debugging OS 3.0 - 3.1.3

+47
objective-c iphone cocoa-touch uikit keyboard
May 13 '10 at 17:18
source share
4 answers

I did a small test project using only UITextField and this code

 #import <UIKit/UIKit.h> @interface TextFieldTestViewController : UIViewController <UITextFieldDelegate> { UITextField *textField; } @property (nonatomic, retain) IBOutlet UITextField *textField; @end #import "TextFieldTestViewController.h" @implementation TextFieldTestViewController @synthesize textField; - (void)viewDidLoad { [self.textField setDelegate:self]; [self.textField setReturnKeyType:UIReturnKeyDone]; [self.textField addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit]; [super viewDidLoad]; } - (IBAction)textFieldFinished:(id)sender { // [sender resignFirstResponder]; } - (void)dealloc { [super dealloc]; } @end 

A text field is an unmodified UITextField , draggable to the NIB, with a plugged in socket.
After loading the application, clicking in the text field brings up the keyboard. When you click Finish, the text field loses focus and enlivens the keyboard. Note that network advice should always use [sender resignFirstResponder] , but this works without it.

+89
May 13 '10 at 21:09
source share

In Xcode 5.1

Enable Done Button

  • In the Attributes Inspector for UITextField in the Storyboard, find the "Return Key" field and select "Done"

Hide keyboard when Finish button is pressed

  • In Storyboard, make your ViewController a delegate for UITextField
  • Add this method to your ViewController

     -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } 
+13
Sep 02 '14 at 13:25
source share

From the documentation (any version):

It is your responsibility to reject the keyboard during your selection. You can reject the keyboard in response to a specific user action, for example, to the user by clicking a specific button in your user interface. You can also configure the text field delegate to release the keyboard when the user presses the return key on the keyboard itself. To dismiss the keyboard, send a message to the resignFirstResponder text box, which is currently the first Responder. This calls the text object field to end the current editing session (with delegate objects) and hide the keyboard.

So you have to send resignFirstResponder somehow. But there is a chance that the text field loses focus in a different way during the processing of textFieldShouldReturn: message. This will also cause the keyboard to disappear.

+6
May 13 '10 at 17:29
source share

One line code for the Finish button: -

 [yourTextField setReturnKeyType:UIReturnKeyDone]; 

And add the action method to valueChanged from TextField and add this line -

 [yourTextField resignFirstResponder]; 
+2
Mar 29 '13 at 11:13
source share



All Articles