我在我的一个 View Controller 中设置了一个 iCarousel,我想简单地将用户点击的相同图像传递到下一个 View Controller 。这是我在 Carousel VC 上的 prepareForSegue 中的内容:
var padIndex = [UIImage]()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let nextVC = segue.destination as! BlackViewController
let indexPath = sender as! NSIndexPath
nextVC.pad = self.images[indexPath.row]
}
然后在我的第二个 VC 中我有:
var pad = UIImage()
@IBOutlet weak var padImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
padImage.image = pad
}
看起来很简单,但我一直收到错误消息:
Could not cast value of type 'SampleSequencer.ChooseViewController' (0x10213fe38) to 'NSIndexPath' (0x1025b8440).
提前十亿表示感谢!!
最佳答案
异常告诉您您进行了非法转换。错误在这里:
let indexPath = sender as! NSIndexPath
prepare(for:sender:)
的sender
参数说它可以是Any?
,但实际上,它并不可怕有用,因为你不能确定它会是什么。您必须从 iCarouselDelegate
获取 IndexPath
(如果它实现了 iCarouselDelegate
,它可能是您自己的 View Controller ),使用委托(delegate)的 carousel(_:,didSelectItemAtIndex:)
方法。
关于ios - 如何将图像/数据从 iCarousel 传递到新的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50422241/