如何在 Swift 中将变量分配给 UIInteraction?

问题描述

为什么需要变量?因为在单元格中长按 2 次不同的图像,func contextMenuInteraction

中需要调用 2 个图像

下面是我的代码,我为每个长按交互分配了一个变量。我收到错误 Thread 1: Swift runtime failure: Unexpectedly found nil while implicitly unwrapping an Optional value

//范围内

var dd : UIInteraction!
var cc : UIInteraction!
@IBOutlet weak var immy: UIImageView!

//分别覆盖funcawakeFromNib()和一个objC长按函数

immy.addInteraction(dd) // (this is in the override nib)
self.like.addInteraction(self.cc) //(this is in the @objc func didLongPress())

下面是调用 2 个交互的 func ContextMenuInteraction

    func contextMenuInteraction(_ interaction: UIContextMenuInteraction,configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
    UIContextMenuConfiguration(identifier: nil,previewProvider: {
        
        if self.dd as! NSObject == interaction {
            if let unwrappedImage = self.immy.image {
                return ImagePreviewController(image:unwrappedImage)
                
            }
            else {
                return nil
            }
          // put dd stuff here
        } else if self.cc as! NSObject == interaction {
          // put cc stuff here
            let image3 = UIImage(named:"ring-309550-2.png")

            if let unwrappedImage1 = image3 {
                return ImagePreviewController(image:unwrappedImage1)
                
            }
            else {
                return nil
            }

        }
        else {
                      return nil
                  }
            })
            }

unexpected found nil 错误在哪里发生 - 在这一行中:

immy.addInteraction(dd)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)