ios Swift 3:无法识别的选择器发送到实例 Xcode 8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38344184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:40:57  来源:igfitidea点击:

Swift 3: Unrecognized selector sent to instance Xcode 8

iosswiftswift3uigesturerecognizer

提问by Raul

I have programmatically created an UIViewand added UIPanGestureRecognizerto it:

我以编程方式创建了一个UIView并添加UIPanGestureRecognizer到其中:

class ViewController: UIViewController{
    var preludeView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        initViews()
        createConstrants()

        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector(("handleTap:")))
        preludeView.addGestureRecognizer(panGestureRecognizer)
    }

    func handleTap(recognizer: UIPanGestureRecognizer) {
        print("WORKING!!!!")
    }

    func initViews() {
        ...
    }

    func createConstrants() {
        ...
    }
}

But when I am touching the view Xcode is throwing an error:

但是当我触摸视图时,Xcode 抛出一个错误:

2016-07-13 09:24:29.918 Draft_Hypa_02[661:83024] -[Draft_Hypa_02.ViewController handleTap:]: unrecognized selector sent to instance 0x17d94a10 2016-07-13 09:24:29.921 Draft_Hypa_02[661:83024] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Draft_Hypa_02.ViewController handleTap:]: unrecognized selector sent to instance 0x17d94a10' *First throw call stack: (0x249cf91b 0x2416ae17 0x249d52b5 0x249d2ee1 0x248fe238 0x294ae9eb 0x290e984f 0x28f7aff1 0x294afd4f 0x28f3ba57 0x28f38017 0x28f78ec9 0x28f7867b 0x28f49125 0x28f476d3 0x24991dff 0x249919ed 0x2498fd5b 0x248df229 0x248df015 0x25ecfac9 0x28fb1189 0x93144 0x24587873) libc++abi.dylib: terminating with uncaught exception of type NSException

2016年7月13日09:24:29.918 Draft_Hypa_02 [661:83024] - [Draft_Hypa_02.ViewController handleTap:]:无法识别的选择发送到实例0x17d94a10 2016年7月13日09:24:29.921 Draft_Hypa_02 [661:83024] *终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因是: ' - [Draft_Hypa_02.ViewController handleTap:]:发送到实例0x17d94a10无法识别的选择' *第一掷调用堆栈:(0x249cf91b 0x2416ae17 0x249d52b5 0x249d2ee1 0x248fe238 0x294ae9eb 0x290e984f 0x28f7aff1 0x294afd4f 0x28f3ba57 0x28f38017 0x28f78ec9 0x28f7867b 0x28f49125 0x28f476d3 0x24991dff 0x249919ed 0x2498fd5b 0x248df229 0x248df015 0x25ecfac9 0x28fb1189 0x93144 0x24587873) libc++abi.dylib:NSException 类型异常终止

However, if I remove argument in the handleTapfunction and remove the colon in the Selector(("handleTap:")), everything works fine!

但是,如果我删除函数中的handleTap参数并删除 中的冒号,则Selector(("handleTap:"))一切正常!

Already spent a day to try to fix this issue and would be very appreciate for your help!

已经花了一天时间尝试解决此问题,非常感谢您的帮助!

回答by Nirav D

If you are using swift3 your selectorshould be like this

如果你使用swift3 你selector应该是这样的

#selector(self.handleTap(recognizer:))

回答by Mahendra Y

You should write this statement in different way. use following line

您应该以不同的方式编写此语句。使用以下行

let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:)))

让 panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:)))

instead of

代替

let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector(("handleTap:")))

让 panGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector(("handleTap:")))

New Way

新方法

let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(YourViewController.handleTap(_:)))

让 panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(YourViewController.handleTap(_:)))

回答by Diogo Antunes

class ViewController: UIViewController {

    let customView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handleTap(_:)))
        customView.addGestureRecognizer(panGestureRecognizer)
    }

    func handleTap(panGesture: UIPanGestureRecognizer) {

    }
}

Don't forget to add your custom UIView to the VC.

不要忘记将您的自定义 UIView 添加到 VC。