我在Objective-C中有以下代码需要转换为Dwift。
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
[self addGestureRecognizer:tap];
}
return self;
}
我尝试了以下方法,但选择器未被触发:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let x:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped))
}
这里有什么问题?
答案 0 :(得分:0)
您的Objective-C代码调用addGestureRecognizer
。你的Swift代码没有。
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let x:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped))
addGestureRecognizer(x)
}