0

I have a UITextView which I am using as text entry in a chat application. When the user presses return then I want to do some action e.g. save the chat message. I haven't been able to find a solution that allows me to do this (lots for TextFields but not for TextView). Here is the solution I am trying at the moment, which seems to be the most obvious I can find, but it isnt working, in debug I see that the method isn't touched:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        NSLog(@"Return pressed");
    } else {
        NSLog(@"Other pressed");
    }
    return YES;
}

In my chat .h file:

@interface ChatTableViewController : UITableViewController <UITextViewDelegate>

and viewdidload .m file

- (void)viewDidLoad {
    [super viewDidLoad];

    enterText.delegate = self;
    // more
}

Incidentally if there is a better field to use as input in a chat program than UITextView please let me know.

Many thanks

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
Kitcc
  • 2,138
  • 4
  • 24
  • 42

2 Answers2

0

Try this way. No need to go with delegates.

Add this event responder at where you initialize the textView.

[theTextView addTarget:self
                action:@selector(targetMethodToPerformCustomOperation)
      forControlEvents:UIControlEventEditingDidEndOnExit];
Alex Andrews
  • 1,498
  • 2
  • 19
  • 33
0

... but it isn't working, in debug I see that the method isn't touched

Your problem obviously is that the delegate method is not called. Fix the delegate and use the code you already have: it's good.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200