RobertJoseph RobertJoseph - 1 month ago 11
iOS Question

UITextField: force resignFirstResponder when tapped outside of it

I have a static

UITableView
that contains a number of
UITextField
s. When the user taps outside of any of the text fields I'd like to I'd like to dismiss the keyboard.

At the top level of my
UIViewController
is a
UITableView
and a
UITabBarItem
. I believe I'll also have to handle taps on the status bar.

I am unsure as how I should register for touches on them (so that I can force any of the text fields to call
resignFirstResponder
.

I thought I might have to handle
UIResponder
's

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event


for the table view and tab bar item but neither of them are
UIControl
s so I can't use

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents


UIWindow
is also a
UIResponder
but again, I can't seem to get touch events for it.

Any help would be much appreciated,
CS

Answer

When you have a static UITableView it "eats" the touch events. So what I did was by subclassing UITableView and then added a delegate which reports touch events.

Comments