我需要你的帮助!我试图将我的第一个ViewController中选定的PHAsset数组传递给下一个。 我在这里声明了数组:
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]()
在这里添加名为BSImagepicker的pod的PHAssets:
@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)
}
然后我使用准备segue函数来传递数组:
override func prepare(for segue: UIStoryboardSegue, sender: Any? ) {
if (segue.identifier == "SegueToViewControllerGame") {
(segue.destination as! ViewControllerGame).SelectedAssetsGame = (SelectedAssets as AnyObject) as! [PHAsset]
}
}
在第二个ViewController中,我想将数组SelectedAssets保存在数组SelectedAssetsGame中:
class ViewControllerGame: UIViewController {
var SelectedAssetsGame: [PHAsset] = []
我总是得到NSInvalidArgumentException
。我该怎么办?
非常感谢!
答案 0 :(得分:0)
在btnSelectPhotos
方法中,您可以简单地将资产分配到SelectedAssets
变量,而不是执行for循环:
finish: { (assets: [PHAsset]) -> Void in
self.SelectedAssets = assets
}
然后将SelectedAssets
对象传递给目标视图控制器:
override func prepare(for segue: UIStoryboardSegue, sender: Any? ) {
if (segue.identifier == "SegueToViewControllerGame") {
(segue.destination as! ViewControllerGame).SelectedAssetsGame = SelectedAssets
}
}
编辑(解决方案):
确保已正确连接IBOutlets
(没有任何旧的无效插座)并将正确的参数传递给方法调用。