【问题标题】:App is crashing because sender is unrecognized [duplicate]应用程序崩溃,因为发件人无法识别[重复]
【发布时间】:2018-05-13 12:43:58
【问题描述】:

这是我为 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

【问题讨论】:

    标签: ios swift uitapgesturerecognizer crash


    【解决方案1】:

    在您的操作函数中,您的参数类型不正确,它必须是 UIGestureRecognizer 对象。像这样修复它:

    @objc private func openImageGallery(_ sender: UITapGestureRecognizer) {
        // Your code
    }
    

    您不能调用tag 属性,因为手势识别器没有这样的属性。您必须引用它的 view 属性才能实现目标:

    print(sender.view!.tag)
    

    【讨论】:

    • 实际上我这样做了,并且错误指出 UITapGestureRecognizer 没有标签。但我设法让它发挥作用。
    • @Kira 观看第二个代码 sn-p。我们不在 GR 上调用 tag,我们在它的视图上调用它。
    猜你喜欢
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 2014-08-25
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2014-09-19
    相关资源
    最近更新 更多