Skip to content

Instantly share code, notes, and snippets.

@PsychoH13
Created November 29, 2010 04:30
Show Gist options
  • Save PsychoH13/719584 to your computer and use it in GitHub Desktop.
Save PsychoH13/719584 to your computer and use it in GitHub Desktop.
#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
#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