无法识别的选择器发送到实例Swift 3

时间:2016-10-14 04:55:07

标签: ios swift3 xcode8 uitapgesturerecognizer unrecognized-selector

我想用TapGestureRecognizer实现selector,下面是我将tapGestureRecognizer添加到imageView

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

这是动作方法

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

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

编译器一直在说

  

[UITapGestureRecognizer tag]:无法识别的选择器发送到实例0x6080001a7700'

1 个答案:

答案 0 :(得分:9)

问题在于您的方法声明以及为tag的对象分配UIGestureRecognizer。像这样更改方法声明。

func assignImage(_ sender: UITapGestureRecognizer)

func assignImage(_ sender: UIGestureRecognizer)

修改:使用UITapGestureRecognizer访问imageView对象。

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

    }
}

注意:您需要tagimageView一起设置UITapGestureRecognizer