Unrecognized selector sent to instance Swift 3

本小妞迷上赌 提交于 2019-11-27 06:26:56

问题


I want to implement TapGestureRecognizer with the selector, below is the code where I added tapGestureRecognizer to my imageView

let tapFirstGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(assignImage(_:)))
firstImageView.isUserInteractionEnabled = true
firstImageView.tag = 1
firstImageView.addGestureRecognizer(tapFirstGestureRecognizer)

Here is the action method

func assignImage(_ sender: UIImageView){
    imagePicker.allowsEditing = false
    imagePicker.sourceType = .photoLibrary
    imageViewTag = sender.tag

    present(imagePicker, animated: true, completion: nil)
}

Compiler keeps saying

[UITapGestureRecognizer tag]: unrecognized selector sent to instance 0x6080001a7700'


回答1:


The problem is with your method declaration and with assigning tag to the object of UIGestureRecognizer. Change your method declaration like this.

func assignImage(_ sender: UITapGestureRecognizer)

Or

func assignImage(_ sender: UIGestureRecognizer)

Edit: To access imageView object with UITapGestureRecognizer.

func assignImage(_ sender: UITapGestureRecognizer) {
    if let imageView = sender.view as? UIImageView {

    }
}

Note: You need to set tag with you imageView not with you UITapGestureRecognizer



来源:https://stackoverflow.com/questions/40035172/unrecognized-selector-sent-to-instance-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!