Created
November 29, 2010 04:30
-
-
Save PsychoH13/719584 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
#import <UIKit/UIKit.h> | |
@interface UIControl (PSYBlockTargetAddition) | |
- (id)addTargetForControlEvents:(UIControlEvents)controlEvents usingBlock:(void(^)(id sender, UIEvent *event))block; | |
- (void)removeTarget:(id)target forControlEvents:(UIControlEvents)controlEvents; | |
@end |
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
#import "UIControl+BlockTargetAddition.h" | |
@interface PSYBlockTargetHelper : NSObject | |
{ | |
@private | |
void(^_block)(id sender, UIEvent *event); | |
} | |
- (id)initWithBlock:(void(^)(id sender, UIEvent *event))block; | |
- (IBAction)PSY_action:(id)sender forEvent:(UIEvent *)event; | |
@end | |
@implementation UIControl (PSYBlockTargetAddition) | |
- (id)addTargetForControlEvents:(UIControlEvents)controlEvents usingBlock:(void(^)(id sender, UIEvent *event))block; | |
{ | |
id helper = [[PSYBlockTargetHelper alloc] initWithBlock:block]; | |
[self addTarget:helper action:@selector(PSY_action:forEvent:) forControlEvents:controlEvents]; | |
return helper; | |
} | |
- (void)removeTarget:(id)target forControlEvents:(UIControlEvents)controlEvents; | |
{ | |
[self removeTarget:target action:@selector(PSY_action:forEvent:) forControlEvents:controlEvents]; | |
[target release]; | |
} | |
@end | |
@implementation PSYBlockTargetHelper | |
- (id)init | |
{ | |
[self release]; | |
return nil; | |
} | |
- (id)initWithBlock:(void(^)(id sender, UIEvent *event))block; | |
{ | |
if(block == nil) return [self init]; | |
if((self = [super init])) | |
{ | |
_block = [block copy]; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[_block release]; | |
[super dealloc]; | |
} | |
- (IBAction)PSY_action:(id)sender forEvent:(UIEvent *)event; | |
{ | |
if(_block != nil) _block(sender, event); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment