I am defining this method in my UITableViewController subclass:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSLog(@"touch began");
}
but it's not being triggered. UIViewController is a subclass of UIResponder so it should work, right? Thanks
Yep, that should definitely work.
Make sure User Interaction Enabled is set for the view this controller corresponds to.
If the view is loaded from a nib/xib, make sure File's Owner is set to be the appropriate class name, and File's Owner's
view
outlet is connected to proper the view.Update: I see this behavior as well, using an app built with the Nav-based App template, but with a View-based App template, it works as expected.
I think that in the case of a Navigation Controller, the table view embedded in the view controller is getting the event before the view controller does. See the UIView reference (emphasis mine):
This implies that the view has the first chance to handle the event, and if it does, the view controller won't get it.
Not exactly clear what you're trying to accomplish, so I'm not sure what solution to offer.
If you are trying to dismiss a keyboard in a UITableViewController, a decent solution is to call resignFirstResponder in the tableView delegate's didSelectRowAtIndexPath method.
I just ran into the exact issue as the original posting. I got around the issue by creating a subclass of UITableView and overriding the "touchesBegan" method. I also created a protocol so the UITableView could call a method on the UITableViewController, which is ultimately where I wanted to handle the "touchesBegan" event.
Here is the header for UITableView subtype:
And implementation:
And finally, the code fragments from UITableViewController:
Hope this helps...
You need to pass the touches up the responder chain. We can do this by subclassing UITableView and overriding touchesBegan (and others if needed) and then passing the touches up the responder chain to the UITableViewController.
UITableViewTouch.h:
UITableViewTouch.m:
Swift Version because I just had this very issue: