首页 > 解决方案 > Swift:使用present时无法在视图控制器之间传递数据

问题描述

我已经以编程方式实现了这些。

表视图单元:

class TableViewCell: UITableViewCell{

    var moviesItems: [Document] = []

    @objc func didPressVoirToutButton() {
        print("Voir tout button was pressed.")
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
           let vc: VoirToutViewController = storyboard.instantiateViewController(withIdentifier: "VoirToutViewController") as! VoirToutViewController
           self.viewController?.present(vc, animated: false, completion: nil)
           print("moviesItemsArray being communicated to moviesItems",moviesItems) 
           // moviesItems is not empty
            vc.voirToutMoviesItemsArray=self.moviesItems
    }
}

VoirToutViewController:

class VoirToutViewController: UIViewController {
    var voirToutMoviesItemsArray : [Document]?
}

我正在尝试通过将其设置为moviesItemsTableViewControllerto进行通信。VoirToutViewControllervoirToutMoviesItemsArray

问题始终 VoirToutViewController.voirToutMoviesItemsArray是空的。
知道我做错了什么吗?

标签: iosswiftuiviewcontrollerviewcontrollertvos

解决方案


它是 nil ,因为您在初始化它的值之前呈现了 vc。修理:

放置这条线

vc.voirToutMoviesItemsArray=self.moviesItems

在这之前

self.viewController?.present(vc, animated: false, completion: nil)


推荐阅读