ResignFirstResponder when clicking UITableView?

I have a UITableView with two custom cells, and each one has a subview with a UITextField inside it. I tried adding a UIButton on top of the UITableView and having it resignFirstResponder , but that just means you can't use it anywhere - even on UITextFields , to enter text.

How to do this, if I touch these two UITextFields , can I call resignFirstResponder so that the user can return to other cells?

thanks

+3
source share
3 answers

I asked a similar question and find the answer myself , which I posted. Basically you can override hitTest: withEvent: in your custom UITableViewCell, which allows you to detect when a touch is detected in your cell or any other cell that is in the table view. I gave an example of the hitTest: method, which duplicates the functionality of the original hitTest: and you can just add what you need. You can add your resignFirstResponder inside the hitTest function, as I did, or configure it further.

+1
source

A good easy way to do this would be to subclass UITableView and implement sensory methods (remember to pass them super super!). From there, you can inspect the touch by touch and decide if it will touch inside the UITextField (using the hitTest: method in UIView). If hitTest does not return a UITextField, you can call the method in your controller to leave the first responder. I hope this indicates that you are in the right direction!

+2
source

Instead of trying to go beyond the field to cancel the first responder, just resize the table view to -setFrame when the keyboard is displayed so that it is in the visible area above the keyboard. Then you can scroll the view to other cells. You can receive notifications that the keyboard is visible by registering for an event as follows:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

Then we implement keyboardWillShow:

 - (void)keyboardWillShow:(NSNotification*)notification; { [tableView setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 200.0f)]; } 

Best wishes.

-2
source

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


All Articles