在MVVM中的viewController之间传递数据

时间:2019-05-24 11:37:29

标签: ios swift mvvm

我无法将数据从我的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
    }
}

0 个答案:

没有答案