Hiding the Keyboard when losing focus on a UITextView

Viewed 42094

So I have a UITextView that I'm using to allow the user to submit some text.

My problem is, I can't seem to figure out how to allow the user to 'Cancel' by tapping away from the UITextView.

10 Answers

My way to do it...

  • Add a reference to the TextView in the @interface of your ViewController:

    @property (weak, nonatomic) IBOutlet UITextView *yourTextView;
    
  • Override this method in the @implementation of your ViewController:

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        if ([self.yourTextView isFirstResponder]) {
          [self.yourTextView resignFirstResponder];
        }
    }
    
Related