gpt4 book ai didi

iphone - UIButton block 相当于 addTarget :action:forControlEvents: method?

转载 作者:行者123 更新时间:2023-12-03 18:09:40 26 4
gpt4 key购买 nike

我环顾四周,但在互联网上和苹果文档中都找不到这个,所以我猜它不存在。

但是 iOS4 是否有一个等效的 API 可以:

[button addTarget:self action:@selector(tappy:) forControlEvents:UIControlEventTouchUpInside];

我想这可以使用类别来实现,但由于极端懒惰,我宁愿不自己编写这个:)

像这样的事情会很棒:

[button handleControlEvent:UIControlEventTouchUpInside withBlock:^ { NSLog(@"I was tapped!"); }];

最佳答案

我刚刚实现了这个。它就像一个魅力!

而且这并不难。

typedef void (^ActionBlock)();

@interface UIBlockButton : UIButton {
ActionBlock _actionBlock;
}

-(void) handleControlEvent:(UIControlEvents)event
withBlock:(ActionBlock) action;
@end

@implementation UIBlockButton

-(void) handleControlEvent:(UIControlEvents)event
withBlock:(ActionBlock) action
{
_actionBlock = action;
[self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];
}

-(void) callActionBlock:(id)sender{
_actionBlock();
}
@end

关于iphone - UIButton block 相当于 addTarget :action:forControlEvents: method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3908003/

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