【问题标题】:Pass array from AppDelegate to view controller将数组从 AppDelegate 传递到视图控制器
【发布时间】:2018-09-01 21:10:31
【问题描述】:

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

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

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

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 为什么不从根视图控制器加载数据?
  • 考虑applicationWillFinishLaunching 可以比主视图控制器的viewDidLoad 更晚被调用????

标签: arrays swift appdelegate


【解决方案1】:

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多