Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

ios - Unrecognized selector sent to instance Swift 3

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'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...