以下是我在viewController
中为所有图像添加识别器的代码 var index = 0
for imageView in imageViews {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.openImageGallery(_:)))
imageView.addGestureRecognizer(tapGestureRecognizer)
imageView.isUserInteractionEnabled = true
imageView.tag = index
index = index + 1
}
这是我触发的方法
@objc private func openImageGallery(_ sender: UIImageView) {
if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum) {
Alertift.actionSheet(message: nil)
.actions(["Choose Image", "Remove"])
.action(.cancel("Cancel"))
.finally { action, index in
if action.style == .cancel {
return
}
if index == 0 {
self.present(self.imagePicker, animated: true, completion: nil)
}
if index == 1 {
print(sender.tag)
}
}
.show()
}
}
所以这里发生的是,用户点击imageView和操作表打开。它有2个选项。删除选项是我感兴趣的东西。我想删除之前拍过的imageView中的图像。
我想我可以使用标签,但它不起作用。应用程序崩溃。有什么想法吗?
2018-05-12 16:52:32.645088+0300 Lez[6027:2387148] *** Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer tag]: unrecognized selector sent to instance 0x1c03e0200'
*** First throw call stack:
(0x1839e2d8c 0x182b9c5ec 0x1839f0098 0x1839e85c8 0x1838ce41c 0x10501bd78 0x104fdfe84 0x1057fe4a4 0x1057ff9c0 0x10580e518 0x1057fd050 0x105807a98 0x1057fef88 0x1057fce90 0x18d79e0c0 0x18dbb5eec 0x18d7aa3cc 0x18da3bcbc 0x18d5adc04 0x18d5ad7ac 0x18d5ad134 0x18d697048 0x18d6970e8 0x187b68774 0x108ea1220 0x108ea5db0 0x18398b070 0x183988bc8 0x1838a8da8 0x18588b020 0x18d88978c 0x105030178 0x183339fc0)
libc++abi.dylib: terminating with uncaught exception of type NSException
答案 0 :(得分:4)
在你的动作函数中你有一个不正确的参数类型,它必须是一个UIGestureRecognizer
对象。修复如下:
@objc private func openImageGallery(_ sender: UITapGestureRecognizer) {
// Your code
}
您无法调用tag
属性,因为手势识别器没有这样的属性。您必须引用它的view
属性才能实现目标:
print(sender.view!.tag)