I need your help! I am trying to pass an array of selected PHAssets from my first ViewController to the next one. I am declaring the array here:
import UIKit
import Photos
import BSImagePicker
class ViewControllerSelectPhotos: UIViewController {
@IBOutlet weak var btnSelect: UIButton!
@IBOutlet weak var btnStartGame: UIButton!
@IBOutlet weak var lblStatus: UILabel!
var SelectedAssets = [PHAsset]()
And add the PHAssets with a pod named BSImagepicker here:
@IBAction func btnSelectPhotos(_ sender: UIButton) {
let vc = BSImagePickerViewController()
self.bs_presentImagePickerController(vc, animated: true, select: { (asset: PHAsset) -> Void in
}, deselect: { (asset: PHAsset) -> Void in
}, cancel: { (assets: [PHAsset]) -> Void in
}, finish: { (assets: [PHAsset]) -> Void in
for i in 0..<assets.count
{ self.SelectedAssets.append(assets[i])
}
}, completion: nil)
}
Then I am using the prepare for segue function to pass the array:
override func prepare(for segue: UIStoryboardSegue, sender: Any? ) {
if (segue.identifier == "SegueToViewControllerGame") {
(segue.destination as! ViewControllerGame).SelectedAssetsGame = (SelectedAssets as AnyObject) as! [PHAsset]
}
}
In the second ViewController I want to save the array SelectedAssets in the array SelectedAssetsGame:
class ViewControllerGame: UIViewController {
var SelectedAssetsGame: [PHAsset] = []
I always get a NSInvalidArgumentException
. What can I do?
Thank you very much!
In your btnSelectPhotos
method, instead of doing a for loop, you can simply assign the assets to your SelectedAssets
variable like this:
finish: { (assets: [PHAsset]) -> Void in
self.SelectedAssets = assets
}
And then pass your SelectedAssets
object to your destination view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any? ) {
if (segue.identifier == "SegueToViewControllerGame") {
(segue.destination as! ViewControllerGame).SelectedAssetsGame = SelectedAssets
}
}
EDIT (Solution):
Make sure you have connected your IBOutlets
properly (without any old invalid outlets) and you pass correct parameters to your method calls.