为我的点击手势发送到实例的无法识别的选择器

Unrecognized selector sent to instance for my tap gesture

我有以下代码

lazy private var _containerView: UIView = {
    let view = UIView(frame: self.view.frame)
    let tapGesture = UITapGestureRecognizer(
        target: self,
        action: Selector(("didtapContainerView:"))
    )
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = UIColor(white: 0.0, alpha: 0)
    view.addGestureRecognizer(tapGesture)
    tapGesture.delegate = self
    return view
}()

这就是方法

@objc final func didtapContainerView(gesture: UITapGestureRecognizer) {
    setDrawerState(state: .Closed, animated: true)
}

我收到了这个错误

Forsa.KYDrawerController didtapContainerView]: unrecognized selector sent to instance

这是 Swift ,将选择器更正为

let tapGesture = UITapGestureRecognizer(target: self,action: #selector(didtapContainerView(_:)))

//

@objc func didtapContainerView(_ gesture: UITapGestureRecognizer) {
    setDrawerState(state: .Closed, animated: true)
}

您不知道如何为此方法制作正确的选择器。 (应该是 "didtapContainerViewWithGesture:",但显然你不知道。)

所以不要尝试。使用 #selector 语法,让编译器为您形成选择器!

就说#selector(didtapContainerView)。完毕。