如何使UIBlockButton适合ARC? [英] How to make UIBlockButton suit with ARC?
本文介绍了如何使UIBlockButton适合ARC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这篇文章中的UIBlockButton
代码:
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
但是我将代码更改为ARC,如何更改代码以确保一切正常?
but I changed my code to under ARC, how to change the code to make sure everything works well?
推荐答案
标题:
@interface UIBlockButton : UIButton
- (void) handleControlEvent: (UIControlEvents) event withBlock: (dispatch_block_t) action;
@end
实施:
@interface UIBlockButton ()
@property(copy) dispatch_block_t actionBlock;
@end
@implementation UIBlockButton
@synthesize actionBlock;
- (void) handleControlEvent: (UIControlEvents) event withBlock: (dispatch_block_t) action
{
[self setActionBlock:action];
[self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];
}
- (void) callActionBlock: (id) sender
{
if (actionBlock) actionBlock();
}
@end
但是请注意,多次调用handleControlEvent:withBlock:
会覆盖您的代码块,使用此实现,您不能对不同的事件采取不同的操作.另外,您可能应该为该类使用不同的前缀而不是UI
,以防止与Apple代码的潜在冲突.
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 of UI
to prevent potential clashes with Apple’s code.
这篇关于如何使UIBlockButton适合ARC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文