【发布时间】:2021-06-08 14:17:15
【问题描述】:
为什么我需要变量?因为在单元格中长按 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!
//分别在覆盖func awakeFromNib()和一个objC长按函数
immy.addInteraction(dd) // (this is in the override nib)
self.like.addInteraction(self.cc) //(this is in the @objc func didLongPress())
下面是调用两个交互的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
}
})
}
意外发现 nil 错误发生在哪里 - 在这一行中:
immy.addInteraction(dd)
【问题讨论】:
-
您发布的代码不完整,因为它没有显示
immy的声明。此外,您没有指定致命错误发生的位置。 -
抱歉,刚刚添加了这些。 immy 只是一个
UIImageView出口。发生错误的行是immy.addInteraction(dd)
标签: ios objective-c swift nsobject uicontextmenuinteraction