Skip to content

Instantly share code, notes, and snippets.

@eralston
Created December 31, 2013 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eralston/8200795 to your computer and use it in GitHub Desktop.
Save eralston/8200795 to your computer and use it in GitHub Desktop.
A delegate for resigning a UITextField when the return key is pressed. It's a corrected version of the code here: http://stackoverflow.com/questions/3186065/uitextfield-resign-first-responder-ios4
@interface ResignOnReturnDelegate : NSObject <UITextFieldDelegate>
@end
@implementation ResignOnReturnDelegate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string isEqualToString:@"\n"]) {
[textField resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment