I have UITableViewCells each with a few UIImageViews. I want to pass the event to the parent UITableViewController so it can act upon it. How can I send information back to the UITableViewController to let it know which UIImageView triggered the event. Using the below code, it seems the UITableViewController touchesEnded is triggered when any of the children UITableViewCells fire an event. Any way to pass information in the UIEvent?
Is there a better way to go about event handling?
//UITableViewCell
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == imageView)
{
[[self nextResponder] touchesEnded:touches withEvent:event];
}
}
//UITableViewController
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
NSLog(@"clicked");
}
For a quick and dirty way, you can use the
objc_setAssociatedObjectandobjc_getAssociatedObjectof ObjC runtime.set the object:
and get the object:
But I don’t recommend this way, it may breaks the MVC. I think your should use the Responder Chain patter to handle this situation, check the documentation for
-sendAction:to:from:forEvent:ofUIApplication.