Skip to content

Instantly share code, notes, and snippets.

@pj4533
Created September 20, 2012 14:16
Show Gist options
  • Select an option

  • Save pj4533/3756210 to your computer and use it in GitHub Desktop.

Select an option

Save pj4533/3756210 to your computer and use it in GitHub Desktop.
Respond to button events with blocks
#import <UIKit/UIKit.h>
@interface UIBarButtonItem (ETBlockActions)
- (id) initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style eventHandler:(void(^)(void))handler;
@end
#import "UIBarButtonItem+ETBlockActions.h"
#import <objc/runtime.h>
@implementation UIBarButtonItem (ETBlockActions)
- (id) initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style eventHandler:(void(^)(void))handler {
class_addMethod([self class], @selector(eventHandler), imp_implementationWithBlock((__bridge void *)(handler)), "v@:");
return [self initWithTitle:title style:style target:self action:@selector(eventHandler)];
}
@end
#import <UIKit/UIKit.h>
@interface UIBarButtonItem (ETBlockActions)
- (id) initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style eventHandler:(void(^)(void))handler;
@end
#import "UIBarButtonItem+ETBlockActions.h"
#import <objc/runtime.h>
@implementation UIBarButtonItem (ETBlockActions)
- (id) initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style eventHandler:(void(^)(void))handler {
class_addMethod([self class], @selector(eventHandler), imp_implementationWithBlock(handler), "v@:");
return [self initWithTitle:title style:style target:self action:@selector(eventHandler)];
}
@end
#import <UIKit/UIKit.h>
@interface UIControl (ETBlockActions)
- (void) addEventHandler:(void(^)(void))handler forControlEvents:(UIControlEvents)controlEvents;
@end
#import "UIControl+ETBlockActions.h"
#import <objc/runtime.h>
@implementation UIControl (ETBlockActions)
- (void) addEventHandler:(void(^)(void))handler forControlEvents:(UIControlEvents)controlEvents {
class_addMethod([self class], @selector(eventHandler), imp_implementationWithBlock((__bridge void *)(handler)), "v@:");
[self addTarget:self action:@selector(eventHandler) forControlEvents:controlEvents];
}
@end
#import <UIKit/UIKit.h>
@interface UIControl (ETBlockActions)
- (void) addEventHandler:(void(^)(void))handler forControlEvents:(UIControlEvents)controlEvents;
@end
#import "UIControl+ETBlockActions.h"
#import <objc/runtime.h>
@implementation UIControl (ETBlockActions)
- (void) addEventHandler:(void(^)(void))handler forControlEvents:(UIControlEvents)controlEvents {
class_addMethod([self class], @selector(eventHandler), imp_implementationWithBlock(handler), "v@:");
[self addTarget:self action:@selector(eventHandler) forControlEvents:controlEvents];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment