将initWithCoder:implementation实现转换为Swift

时间:2017-06-23 15:40:05

标签: ios objective-c swift cocoa-touch uigesturerecognizer

我在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))
    }

这里有什么问题?

1 个答案:

答案 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)
}