Objective-c – UITextView has no events

cocoa-touchiphoneobjective cuikituitextview

I am developing an iPhone application which requires a multiline text field (UITextView) to capture some text.

When the user touches inside the textView it becomes firstResponder and displays the keyboard. What I really need it to do is remove the keyboard when the user is finished. Normally with a text field the return/done button press would signal the end of typing and I would use the delegate to resign first responder. However with a multiline textview I want the user to be able to add a new line so that is not possible.

My next option would be to resign first responder if there is a touch up outside of the text view. The problem here is that there are no events declared on UITextView which I can see in interface builder.

How can I create a multiline text field in an iPhone app which will release first responder at a sensible time when the user is done with it?

Best Answer

You can use a DONE button on the navigation bar:

- (void)viewDidLoad {
      [super viewDidLoad];
  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneAction:)];
}

to dismiss the keyboard, handle the event with something like:

- (void) doneAction: (id) sender{ [textView resignFirstResponder]; }

(remember .h file declarations)