Created
October 9, 2011 18:15
-
-
Save codeswimmer/1273972 to your computer and use it in GitHub Desktop.
iOS: How to automatically close the keyboard when user hits Return key in TextField
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // =================================================================================== | |
| // File's Owner needs to implement UITextFieldDelegate protocol in order for the | |
| // keyboard to automatically close when the user hits the Return/Enter key. | |
| // | |
| @interface MyViewController : UIViewController <UITextFieldDelegate> { | |
| IBOutlet UITextField *userInput; | |
| } | |
| @property (nonatomic, retain) UITextField *userInput; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // =================================================================================== | |
| // File's Owner needs to implement UITextFieldDelegate protocol in order for the | |
| // keyboard to automatically close when the user hits the Return/Enter key. | |
| // | |
| #import "MyViewController.h" | |
| @implementation MyViewController | |
| @synthesize userInput; | |
| - (void)viewDidLoad | |
| { | |
| self.userInput.delegate = self; | |
| } | |
| -(BOOL)textFieldShouldReturn:(UITextField *)theTextField | |
| { | |
| if (theTextField == self.userInput) { | |
| [theTextField resignFirstResponder]; | |
| } | |
| return YES; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment