iOS中UIButton的block

来源:互联网 发布:linux安装armlinuxgcc 编辑:程序博客网 时间:2024/05/21 06:27
UIButton

+block

#import <objc/runtime.h>#import <UIKit/UIKit.h>typedef void (^ActionBlock)();@interface UIButton (block)@property (readonly) NSMutableDictionary *event;- (void) handleControlEvent:(UIControlEvents)controlEvent withBlock:(ActionBlock)action;@end


#import "UIButton+block.h"@implementation UIButton (block)static char overviewKey;@dynamic event;- (void)handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock)block {    objc_setAssociatedObject(self, &overviewKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);    [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];}- (void)callActionBlock:(id)sender {    ActionBlock block = (ActionBlock)objc_getAssociatedObject(self, &overviewKey);    if (block) {        block();    }}@end


0 0