我尝试使用在线转换器将此Objective C代码转换为Swift-4,但有些人未能做到这一点,
typedef void (^ActionBlock)();
## interface of custom class ##
@interface UIBlockButton : UIButton {
ActionBlock _actionBlock;
}
## implementation of custom class ##
@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
这是swift代码的输出 ::: https://ibb.co/fRZFP7
答案 0 :(得分:1)
试试这个
typealias ActionBlock = () -> Void
class UIBlackButton: UIButton {
var actionBlock: ActionBlock = {}
func handleControlEvent(event: UIControlEvents, action: @escaping ActionBlock) {
actionBlock = action
self.addTarget(self, action: #selector(callActionBlock), for: event)
}
@objc func callActionBlock(sender: Any) {
actionBlock();
}
}