I want to be able to send a message as soon as the user touches return on the on-screen keyboard. I have a send UIButton, how do I make it listen for the return key? Thanks!
Asked
Active
Viewed 432 times
0
-
TextField or TextView? – Logan Mar 26 '14 at 17:23
-
You don't click a button when the user taps return on the keyboard. You simply have both events call the same method. – rmaddy Mar 26 '14 at 17:26
-
Ok, updated for textField! – Logan Mar 26 '14 at 17:30
2 Answers
1
Your button Method:
- (void) btnPressed:(id)sender {
{
Add Delegate Stuff if you haven't
@interface YourViewController : UIViewController <UITextFieldDelegate>
And when you call your textField:
yourTextField.delegate = self;
Then, you should just call the button's action method in the delegate shouldReturn
method:
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[self btnPressed:nil];
// whatever else you need
return YES;
}

Logan
- 52,262
- 20
- 99
- 128
0
Hook textFieldShouldReturn:
and send a message to the button or directly to the function that button calls.
If you're using UITextView
, it takes a bit of trickery:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (textView == messageInput) {
if ([text isEqualToString:@"\n"]) {
// your code goes here
return NO;
}
}
return YES;
}