Skip to content

Instantly share code, notes, and snippets.

@junpluse
Created April 26, 2013 10:18
Show Gist options
  • Save junpluse/5466303 to your computer and use it in GitHub Desktop.
Save junpluse/5466303 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface NSObject (ResponderBlock)
- (id)responderBlockForSelector:(SEL)selector;
- (void)setResponderBlock:(id)block forSelector:(SEL)selector;
@end
#import "NSObject+ResponderBlock.h"
#import <objc/runtime.h>
@implementation NSObject (ResponderBlock)
- (id)responderBlockForSelector:(SEL)selector
{
Method method = class_getInstanceMethod([self class], selector);
IMP implementation = method_getImplementation(method);
return imp_getBlock(implementation);
}
- (void)setResponderBlock:(id)block forSelector:(SEL)selector
{
Class class = [self class];
IMP newImplementation = imp_implementationWithBlock(block);
IMP oldImplementation = class_getMethodImplementation(class, selector);
if (oldImplementation) {
class_replaceMethod(class, selector, newImplementation, nil);
} else {
class_addMethod(class, selector, newImplementation, nil);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment