views:

313

answers:

1

I have a UIViewController, and I've added two subviews to its view. One subview is the view of a UIViewController. The other subview is a UITextField.

I need to dismiss the keyboard for the UITextField when the user touches the other view, but I can't figure out how to detect those events. The UIViewController's tableView catches them and breaks the UIResponder chain, so my UIViewController never hears about them. I don't want to subclass everything in the hierarchy just so I can pass the event along up the chain, so what are my options?

I should mention that I'm doing everything programmatically, no IB.

Thanks guys.

A: 

So after digging into it, I don't think there's any other way. The responder chain starts with the UIView that received the touch event, if it's not caught passes to that view's controller, then to its superview and so on.

Obviously subclassing every UIView element in a UITableView is insane overkill for this situation.

What I did was create a transparent "touch shield" view with the same frame dimensions as my table view. When the keyboard expands, I add this view over my table, and when it collapses I remove it. This allows me to intercept those touches before they hit the table.

DougW
But how did you set the view to intercept the touches? When I do that, the touches pass straight through.
Joe
@Joe Wibble - You may need to set the UIView's userInteractionEnabled property to "YES". If it is added to the table view, and covers some area, it will intercept touches.
DougW