0

In my simple iPhone app, I have a UITextField for entering text and UITextView for results. The UITextField uses a number pad since only numbers are allowed. The UITextView is read only but has user interaction enabled. The reason for this is that I want a user to be able to do copy and paste but not changing the result. It is well known that number pad doesn't have a "go" button. So I do resignFirstResponder() in touchesBegan and it works as long as I touch outside of the Text View Area. How do I dismiss the keyboard (number pad) even when the inside of the UITextView is touched?

dividebyzero
  • 1,243
  • 2
  • 9
  • 17
  • Side note - some users may be using an external keyboard so make sure you are properly validating user input into the text field since such users will be able to type letters and other symbols. – rmaddy Jan 24 '13 at 16:29
  • I think this might be what your looking for http://stackoverflow.com/questions/8782664/uitextfield-functionality-with-no-keyboard – Shaun Jan 25 '13 at 00:17

1 Answers1

0

You could add a gesture recognizer to the UITextView. Untested code:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.textView addGestureRecognizer:tap];
[tap release]; // not necessary or allowed under ARC

and then the handler:

- (void)handleTap:(UITapGestureRecognizer *)tap
{
    // resign first responder and whatever else you need to do
}

You may need to disable the gesture recognizer when you don’t need it, in case it interferes with text selection in the read-only UITextView.

Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82