ios - 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]?
}
我正在尝试通过将其设置为moviesItems
从TableViewController
to进行通信。VoirToutViewController
voirToutMoviesItemsArray
问题始终 VoirToutViewController.voirToutMoviesItemsArray
是空的。
知道我做错了什么吗?
解决方案
它是 nil ,因为您在初始化它的值之前呈现了 vc。修理:
放置这条线
vc.voirToutMoviesItemsArray=self.moviesItems
在这之前
self.viewController?.present(vc, animated: false, completion: nil)
推荐阅读
- java - sqlite 中的 checkIfRowExists() 不起作用
- asp.net-core-mvc - ASP.NET Core MVC 在局部视图/ViewComponent 中提交表单
- c++ - 在代码块 (C++) 中调试时,我可以选择跳过所有打开的内部库调用和函数吗?
- python - 三元图作为子图
- git - 如何使用 git diff 作为 difftool?
- python - Python + Scrapy + JSON + XPath:如何使用 Scrapy 抓取 JSON 数据
- unity3d - Unity offscreen target indicator-how to show the way to an object with a specific tag?
- css - 我找不到适用于此滑块的 css 类
- model-checking - NuSMV:如何排除可能的下一个状态
- javascript - 在不使用多个循环的情况下按标题查找重复数组