I have an iCarousel set up in one of my view controllers, and I would like to simply pass the same image that the user taps onto the next view controller. Here is what I have in my prepareForSegue on the Carousel VC:
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]
}
Then in my secondVC I have:
var pad = UIImage()
@IBOutlet weak var padImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
padImage.image = pad
}
Seems like a simple thing, but I keep getting an error that says:
Could not cast value of type 'SampleSequencer.ChooseViewController' (0x10213fe38) to 'NSIndexPath' (0x1025b8440).
Thanks a billion in advance!!
The exception is telling you that you've got an illegal cast. The error is here:
let indexPath = sender as! NSIndexPathThe
senderargument ofprepare(for:sender:)says that it can beAny?, but in reality, it's not terribly useful because you can't be sure what it's going to be. You have to get theIndexPathfrom theiCarouselDelegate(which is probably your own view controller, if it implementsiCarouselDelegate), using the delegate'scarousel(_:,didSelectItemAtIndex:)method.