0

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!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Zach Lucas
  • 1,631
  • 5
  • 15
  • 29

2 Answers2

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;
}
Community
  • 1
  • 1
esqew
  • 42,425
  • 27
  • 92
  • 132