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

时间:2018-07-22 22:26:10

标签: ios swift unrecognized-selector

我有以下代码

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]:无法识别的选择器已发送到实例

2 个答案:

答案 0 :(得分:2)

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

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

只需说#selector(didtapContainerView)。做完了

答案 1 :(得分:0)

这是Swift,将选择器更正为此

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

//

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