首页 > 解决方案 > 将数组从 AppDelegate 传递到视图控制器

问题描述

我在我的 AppDelegate 中加载一个包含 Firebase 数据库数据的数组,因为我需要在创建和加载视图之前加载数组。如何将此数组传递给视图控制器以填充 TableView?

编辑: 我认为我的问题措辞不正确,也没有提供足够的信息。我有需要加载到数组中的 Firebase 数据。该数组需要在被使用该数组的视图控制器使用之前加载到应用程序中。这是因为视图控制器使用可可豆荚将数组拆分为多个类别以显示在不同的表格视图中。我正在使用的可可豆荚的 repo 可以在这里找到。

那么我的问题是加载这个数组的最佳位置在哪里?我的第一个想法是 AppDelegate,但数组是空的,因此表视图不会加载。我对 iOS 编程很陌生,所以我愿意接受任何和所有建议。

标签: arraysswiftappdelegate

解决方案


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
}

推荐阅读