Skip to main content
added 144 characters in body
Source Link
ToddB
  • 2.5k
  • 3
  • 24
  • 43

Add an observer in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(textViewKeyPressed:) name: UITextViewTextDidChangeNotification object: nil]; 

and then use the selector to check for "\n"

-(void) textViewKeyPressed: (NSNotification*) notification { if ([[[notification object] text] hasSuffix:@"\n"]) { [[notification object] resignFirstResponder]; } } 

It does use "\n" and not specifically check for a return key, but I think this is OK.

UPDATE

See ribto's answer below which uses [NSCharacterSet newlineCharacterSet] in place of \n

Add an observer in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(textViewKeyPressed:) name: UITextViewTextDidChangeNotification object: nil]; 

and then use the selector to check for "\n"

-(void) textViewKeyPressed: (NSNotification*) notification { if ([[[notification object] text] hasSuffix:@"\n"]) { [[notification object] resignFirstResponder]; } } 

It does use "\n" and not specifically check for a return key, but I think this is OK.

Add an observer in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(textViewKeyPressed:) name: UITextViewTextDidChangeNotification object: nil]; 

and then use the selector to check for "\n"

-(void) textViewKeyPressed: (NSNotification*) notification { if ([[[notification object] text] hasSuffix:@"\n"]) { [[notification object] resignFirstResponder]; } } 

It does use "\n" and not specifically check for a return key, but I think this is OK.

UPDATE

See ribto's answer below which uses [NSCharacterSet newlineCharacterSet] in place of \n

Source Link
ToddB
  • 2.5k
  • 3
  • 24
  • 43

Add an observer in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(textViewKeyPressed:) name: UITextViewTextDidChangeNotification object: nil]; 

and then use the selector to check for "\n"

-(void) textViewKeyPressed: (NSNotification*) notification { if ([[[notification object] text] hasSuffix:@"\n"]) { [[notification object] resignFirstResponder]; } } 

It does use "\n" and not specifically check for a return key, but I think this is OK.