arrays - 将数组从 AppDelegate 传递到视图控制器
问题描述
我在我的 AppDelegate 中加载一个包含 Firebase 数据库数据的数组,因为我需要在创建和加载视图之前加载数组。如何将此数组传递给视图控制器以填充 TableView?
编辑: 我认为我的问题措辞不正确,也没有提供足够的信息。我有需要加载到数组中的 Firebase 数据。该数组需要在被使用该数组的视图控制器使用之前加载到应用程序中。这是因为视图控制器使用可可豆荚将数组拆分为多个类别以显示在不同的表格视图中。我正在使用的可可豆荚的 repo 可以在这里找到。
那么我的问题是加载这个数组的最佳位置在哪里?我的第一个想法是 AppDelegate,但数组是空的,因此表视图不会加载。我对 iOS 编程很陌生,所以我愿意接受任何和所有建议。
解决方案
在AppDelegate
课堂上:
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
//Fill your array, let's say it's called firebaseArray. I would recommend doing so in the view controller and not in the Appdelegate for better responsibility distribution in your code
let vc = YourViewController()
//And YourViewController must have a array property
vc.array = firebaseArray
window = UIWindow(frame: UIScreen.main.bounds)
//make your vc the root view view controller
window?.rootViewController = vc
window?.makeKeyAndVisible()
return true
}
推荐阅读
- javascript - 使用 reactjs 下载文件无法正常工作(使用 axios)
- google-app-engine - 在 GEE 中将 GPM 每小时数据汇总到每天
- bash - 将命令的文本响应转换为二进制 0 或 1
- go - 更新,选择原子。戈尔姆
- python - 如何在熊猫数据框中迭代行以查找特定条件
- python - Django 应用程序在 Google App Engine 上冻结
- python - 计算以每个数据点为中心的固定半径球内数据点数量的有效方法
- python - Tensorflow 无法在 Windows 和 pycharm 上运行
- typescript - 为什么 TypeScript 渐进式类型应该停止编译这个有效的代码?
- vba - 在本地目录中创建文件夹后保存 Outlook 附件