gpt4 book ai didi

ios - 如何使用 ARC 制作 UIBlockButton 套装?

转载 作者:行者123 更新时间:2023-11-28 20:33:26 27 4
gpt4 key购买 nike

我正在使用来自 this postUIBlockButton 代码:

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 下,如何更改代码以确保一切正常?

最佳答案

标题:

@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: 将覆盖您的 block ,您不能使用此实现对不同的事件执行不同的操作。此外,您可能应该为该类使用不同的前缀而不是 UI,以防止与 Apple 代码发生潜在冲突。

关于ios - 如何使用 ARC 制作 UIBlockButton 套装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11449194/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com