I have a UITableView with two custom cells and each of them has a subview with a UITextField inside it. I have tried adding a UIButton on top of the UITableView and have it resignFirstResponder but that just means you won't be able to tap anywhere else - not even on the UITextFields to enter text.
How do I make it so if I tap outside those two UITextFields, I can call resignFirstResponder, so the user can get back to the other cells?
Thanks
A nice simple way to do it would be to subclass a UITableView and implement the touch methods (not forgetting to pass them to the super afterwards!). From in there you can inspect a touch in touches began and decide whether it's a touch that's inside the UITextField (using the hitTest: method in UIView). If the hitTest does not return a UITextField you can call a method in your controller to resign the first responder. Hope this points you in the right direction!
I asked a similar question and found an answer myself which I posted. Basically you can override hitTest:withEvent: in your custom UITableViewCell which lets you detect when a touch occurred in your cell or any other cell that is in the table view. I provided an example hitTest: method that duplicates the functionality of the original hitTest: and you can just add what you need to it. You can add your resignFirstResponder inside the hitTest function like I did or customize it further.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With