Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created October 9, 2011 18:15
Show Gist options
  • Select an option

  • Save codeswimmer/1273972 to your computer and use it in GitHub Desktop.

Select an option

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
// ===================================================================================
// 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
// ===================================================================================
// 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