ios - UITapGestureRecognizer : "Unrecognized selector sent to instance"

标签 ios iphone swift

我知道这个问题过去被问过很多次,但几乎所有答案都已过时( objective-c )或者它们似乎对我不起作用。

当我尝试点击我的 View 时出现以下异常:

2018-02-05 12:18:30.162887+0100 Shay[28239:1248348] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'-[UIView tabClicked:]: unrecognized selector sent to instance 0x7f88edd02240'

这是我的实现:

//setting up the basic properties and adding Tap gesture
init(view: UIView,index: Int, tabHandler: TabHandler,  tabType: TabType) {

    ....

    //TapRecognizerAction
    let tap = UITapGestureRecognizer(target: rootView, action: #selector(  tabClicked(_:) ))
    tap.delegate = self as UIGestureRecognizerDelegate
    rootView.addGestureRecognizer(tap)

}

@objc public func tabClicked(_ recognizer: UITapGestureRecognizer){
print("WORKING")

}

非常感谢帮助新手!

最佳答案

tabClicked 应该在 rootView 实现中而不是在 self.view 中

你可以试试

  let tap = UITapGestureRecognizer(target: self , action: #selector(  tabClicked(_:) ))

extension UIView
{

 @objc public func tabClicked(_ recognizer: UITapGestureRecognizer){
    print("WORKING")

    }
}

关于ios - UITapGestureRecognizer : "Unrecognized selector sent to instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621610/

相关文章:

iphone - 是否需要自定义实现才能在 iO 中创建通知,例如 Twitter 应用程序使用的 "Tweet Sent"通知?

ios - 每次导航时,内存使用都在不断增加。并在 4-5 分钟后崩溃

swift - 是否可以在运行时在 Swift 中动态执行深度属性比较?

ios - 'NSRangeException',原因 : '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'

ios - Swift:即使我切换到不同的 View Controller ,如何使后台功能保持运行

ios - 是否可以在 Objective c 中打开正在运行的后台应用程序

iOS:UISwitch 无法隐藏 .hidden = YES

ios - SWIFT:按钮不会在点击时消失

iphone - 在 UITableView 中实现页脚的正确方法

ios - Swift UITextField 子类以编程方式处理文本更改