Created
July 30, 2011 13:47
-
-
Save proger/1115541 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// VKBlockSelector.h | |
// | |
#include <objc/runtime.h> | |
#import <Foundation/Foundation.h> | |
/* | |
* note: each file has its own! | |
*/ | |
static const char *BLOCK_SELECTOR_ASSOCIATION = "blockselector"; | |
static void | |
selector_IMP(id self, SEL _cmd, id arg) | |
{ | |
NSDictionary *selinfo = objc_getAssociatedObject(self, BLOCK_SELECTOR_ASSOCIATION); | |
assert(selinfo != nil); | |
NSString *name = NSStringFromSelector(_cmd); | |
void (^block)(id arg) = [selinfo objectForKey:name]; | |
block(arg); | |
} | |
static SEL | |
block_selector(id self, SEL sel, void (^block)(id arg)) | |
{ | |
id block_copy = Block_copy(block); | |
NSMutableDictionary *selinfo = objc_getAssociatedObject(self, BLOCK_SELECTOR_ASSOCIATION); | |
if (selinfo == nil) { | |
selinfo = [[[NSMutableDictionary alloc] init] autorelease]; | |
objc_setAssociatedObject(self, BLOCK_SELECTOR_ASSOCIATION, selinfo, OBJC_ASSOCIATION_RETAIN); | |
} | |
[selinfo setObject:block_copy forKey:NSStringFromSelector(sel)]; | |
[block_copy release]; | |
BOOL ok = class_addMethod([self class], sel, (IMP)selector_IMP, "v@:@"); | |
assert(ok && "method already exists"); | |
return sel; | |
} | |
#if 0 | |
// Usage: | |
UISwipeGestureRecognizer *r1 = [[UISwipeGestureRecognizer alloc] | |
initWithTarget:self action:block_selector(self, @selector(handleSwipeUp:), | |
^(id arg) { | |
[self.playerController volumeUp]; | |
[self release]; // was retained | |
})]; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment