Reputation: 16730
I've read the apple docs on the responder chain and needed to know: How is it do i NSLog the object that was tapped on?
Let's say I have a very complex view controller with multiple views object, when ever i tap on an object (UIButton or what ever..) is there a way to know that particular object that was tapped on? The docs gave a good overview, but wasnt to clear in methods to overwrite.
EDIT: The situation is in testing different apps i have not written. I need a quick way to determine the Object that was tapped on (as many apps have custom controls/object that looks like one thing, but is really another). I was hoping there was some way to intercept the touch event right as it was being sent to UIAppication, and then NSLog it.
Upvotes: 1
Views: 1462
Reputation: 23278
For a button the action method normally has that param,
- (void)action:(id)sender {
Here sender represents the button. You can use it as,
UIButton *button = (UIButton *)sender;
button.hidden = YES;//use the properties of button now
You can also check with the UITouch delegate methods.
For eg:-
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [[event allTouches] anyObject];// or UITouch *touch = [touches anyObject];
CGPoint point = [myTouch locationInView:myViewToCheck];
if (CGRectContainsPoint(myViewToCheck.bounds, point) ) {
//Touch detected on myViewToCheck.
}
Upvotes: 1
Reputation: 21383
You can override -[UIApplication sendAction:to:from:forEvent]
to do what you want:
@implementation MyApplicationSubclass
- (BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event
{
NSLog(@"Sending action %@ from sender %@ to target %@ for event %@", NSStringFromSelector(action), sender, target, event);
return [super sendAction:action to:target from:sender forEvent:event];
}
@end
Put that in a custom subclass of UIApplication. Then, in main.m, change the call to UIApplicationMain()
so that your custom subclass is used:
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([MyApplicationSubclass class]), NSStringFromClass([AppDelegate class]));
}
}
Note that this only works for UIControl subclasses, which send their actions to their targets using this mechanism. If you want to see all touch events going through an app, override -[UIApplication sendEvent:]
instead. In that case, it will be up to you to figure out which object is going to receive the touch. You can do that with by calling -hitTest:
on your main view/window, though keep in mind that that figures out which view the touch lands on, not necessarily which view handles it (views can forward events to other objects, for example). Something like this:
@implementation MyApplicationSubclass
- (void)sendEvent:(UIEvent *)event
{
UIWindow *window = [self keyWindow];
NSSet *touches = [event touchesForWindow:window];
for (UITouch *touch in touches) {
UIView *touchedView = [window hitTest:[touch locationInView:window] withEvent:event];
NSLog(@"Touch %@ received in view %@ for event %@", touch, touchedView, event);
}
[super sendEvent:event];
}
@end
Upvotes: 4
Reputation: 412
Have you had a chance to look at https://github.com/domesticcatsoftware/DCIntrospect.
Straight from github: Introspect is small set of tools for iOS that aid in debugging user interfaces built with UIKit.
It has a logging component that may be of use?
Upvotes: 0