Alex1987
Alex1987

Reputation: 9457

Can't get rid of the keyboard in UITextView

I have a table and in one of the cells I keep a UITextView. When the user taps it the keyboard appears. I was trying various ways to get rid of it. My first approach was to detect a tap on the table cell of the UITextView, but since the text view takes most of it, it's not suitable. Then I tried to add a button to the toolbar and whenever the user presses it, the keybord disappears with resignFirstResponder, but it won't work. It seems that only when I'm in the same view as the UITextView resignFirstResponder works. So how can I get rid of the keyboard from a different view?

Thanks.

Upvotes: 3

Views: 3654

Answers (5)

kossibox
kossibox

Reputation: 221

i've use for that kind of problem, the folowing method

- (BOOL)textFieldShouldReturn: tf_some_text_field {

[tf_some_text_field resignFirstResponder];
[tf_another_text_field_in_the_same_view resignFirstResponder];

return YES;
}

hope it helps...

Upvotes: -1

hanumanDev
hanumanDev

Reputation: 6614

try unchecking the 'Editable' option in the Text View Attributes window in Interface Builder.

this stops the keyboard from showing up if the user clicks on your TextView

Upvotes: 0

Jordan
Jordan

Reputation: 21760

The method below uses the Return Key to dismiss the UITextView Keyboard.

In your Controller, set the UITextView's Delegate to self like this:

myTextView.delegate=self;

Then add this method:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
 replacementText:(NSString *)text
{
 // Any new character added is passed in as the "text" parameter
 if ([text isEqualToString:@"\n"]) {
  // Be sure to test for equality using the "isEqualToString" message
  [textView resignFirstResponder];

  // Return FALSE so that the final '\n' character doesn't get added
  return FALSE;
 }
 // For any other character return TRUE so that the text gets added to the view
 return TRUE;
}

This should work if you're not using the Return key as a true Return key (e.g. adding new line).

Upvotes: 1

John Rudy
John Rudy

Reputation: 37850

Without knowing the size of the controls, nor the intent, it's a bit hard to make a solid recommendation. However, if you're looking for a read-only text view, why not consider a UILabel? If it's going to contain a great deal of read-only text and you need rich formatting, consider a UIWebView with your formatting.

Of course, this answer could be completely inappropriate. Can you clarify your intentions, maybe show a screen shot of the app-in-progress (untapped, of course)?

Upvotes: 0

ennuikiller
ennuikiller

Reputation: 46965

What do you mean "it won't work". If txtView is your UITe4xtView, do you mean that associating the button with an action that has [txtView resignFirstResponder] wont work? I've used this code before and it works for me

Upvotes: 1

Related Questions