我无法将数据从我的collectionViewController
传递到detailViewController
。
我现在正在尝试学习MVVM
,这对我来说有点困惑。在MVC
中很简单,但是在MVVM
中我对此有疑问。
我的MainViewModel
:
struct CatViewModel {
let catImage: String
// Dependency Injection
init(catImage: CatModel) {
self.catImage = catImage.url
}
func detailViewModel(for indexPath: IndexPath) -> DetailsViewModel
{
return DetailsViewModel(catImage: catImage)
}
}
我的detailViewModel
:
struct DetailsViewModel {
let catImage: String
}
我的collectionViewCell
班:
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageViewCell: CustomImageView!
var catViewModel: CatViewModel! {
didSet {
setUpCellImage()
}
}
func setUpCellImage() {
if let cellImageUrl = catViewModel?.catImage {
imageViewCell.loadImageUsingUrlString(urlString: cellImageUrl)
}
}
那么我现在应该如何传递detailViewController
中的数据?在MVC
中很简单,但是在MVVM
中找不到正确的答案/方法。
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let destVC = mainStoryboard.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
self.navigationController?.pushViewController(destVC, animated: true)
}
我的detilViewController
班:
class DetailViewController: UIViewController {
@IBOutlet weak var largeImageView: UIImageView!
var detailViewModel = [DetailsViewModel]()
var detailImage = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
self.largeImageView.image = detailImage
}
}