Skip to content

Instantly share code, notes, and snippets.

@kluivers
Created March 27, 2014 14:16
Show Gist options
  • Save kluivers/9808519 to your computer and use it in GitHub Desktop.
Save kluivers/9808519 to your computer and use it in GitHub Desktop.
Responder forwarding
@implementation UIResponder (ResponderForwarding)
- (BOOL) tryToPerformAction:(SEL)action withObject:(id)object
@end
@implementation UIResponder (ResponderForwarding)
- (BOOL) tryToPerformAction:(SEL)action withObject:(id)object
{
if (!action) {
return NO;
}
id target = [[self nextResponder] targetForAction:action withSender:self];
if (target) {
NSMethodSignature *signature = [[target class] instanceMethodSignatureForSelector:action];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = target;
invocation.selector = action;
[invocation setArgument:&object atIndex:2];
[invocation invoke];
return YES;
}
return NO;
}
@end
@kluivers
Copy link
Author

Usage

[self tryToPerformAction:@selector(doSomething:) withObject:someObject];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment