I am using the UIBlockButton code from this post:
typedef void (^ActionBlock)();
@interface UIBlockButton : UIButton {
ActionBlock _actionBlock;
}
-(void) handleControlEvent:(UIControlEvents)event
withBlock:(ActionBlock) action;
@implementation UIBlockButton
-(void) handleControlEvent:(UIControlEvents)event
withBlock:(ActionBlock) action
{
_actionBlock = Block_copy(action);
[self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];
}
-(void) callActionBlock:(id)sender{
_actionBlock();
}
-(void) dealloc{
Block_release(_actionBlock);
[super dealloc];
}
@end
but I changed my code to under ARC, how to change the code to make sure everything works well?
Header:
Implementation:
But note that multiple calls to
handleControlEvent:withBlock:will overwrite your block, you can’t have different actions for different events with this implementation. Also, you should probably use a different prefix for the class instead ofUIto prevent potential clashes with Apple’s code.