编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

无法识别的选择器发送到实例 Swift 3:Unrecognized selector sent to instance Swift 3

lvgg3271 图像识别 2022-5-7 07:32 7人围观

腾讯云服务器
无法识别的选择器发送到实例 Swift 3的处理方法

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

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) 

这里是操作方法

func assignImage(_ sender: UIImageView){ imagePicker.allowsEditing = false imagePicker.sourceType = .photoLibrary imageViewTag = sender.tag present(imagePicker, animated: true, completion: nil) } 

编译器一直说

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

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

问题解答

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

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) 

或者

func assignImage(_ sender: UIGestureRecognizer) 

使用 UITapGestureRecognizer 访问 imageView 对象.

To access imageView object with UITapGestureRecognizer.

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

注意:你需要用你的imageView设置tag而不是你UITapGestureRecognizer

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

这篇关于无法识别的选择器发送到实例 Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^