Cannot get rid of keyboard in UITextView

I have a table, and in one of them I save a UITextView. When the user picks it up, a keyboard appears. I tried in many ways to get rid of him. My first approach was to detect a click on a UITextView table cell, but since the text view takes up most of it, it doesn't work. Then I tried to add a button to the toolbar, and whenever the user clicks it, keybord disappears with resignFirstResponder, but this will not work. It seems that only when I am in the same view as the UITextView resignFirstResponder. So, how can I get rid of the keyboard from a different view?

Thanks.

+3
source share
5 answers

The method below uses the return key to close the keyboard of the UITextView.

In your controller, set the UITextView delegate yourself for example:

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 do not use the Return key as a true return key (for example, add a new line).

+1
source

What does it mean, it won’t work. If txtView is your UITe4xtView, do you mean that associating a button with an action that has [txtView resignFirstResponder] will not work? I used this code before and it works for me

+1
source

, . , , UILabel? , , , UIWebView .

, . , , (, )?

0

"" " " " ".

, TextView

0

I am using this problem, the following method

- (BOOL)textFieldShouldReturn: tf_some_text_field {

[tf_some_text_field resignFirstResponder];
[tf_another_text_field_in_the_same_view resignFirstResponder];

return YES;
}

hope this helps ...

-1
source

Source: https://habr.com/ru/post/1718227/


All Articles