分享 JSON TabBarController 中的数据以查看控制器

Share JSON Data in TabBarController to view controllers

我正在使用 tabbarcontroller 来显示 3 个 xib。我想解码 UITabBarController 子类中的 JSON 数据,然后与视图控制器共享数据(据我所知,这是执行此操作的首选方法)。我已经在每个视图控制器中单独成功地完成了这个,其中相同的 JSON 数据被分别解码了 3 次,但我现在试图通过只处理一次 JSON 来提高这个过程的效率。

我目前收到以下错误

"Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee7ab7d98)".

下面是我目前使用的代码。我主要只包括第一个视图控制器的代码,但其他视图控制器的代码是一样的

这是其中一个视图控制器。任何帮助将不胜感激,谢谢!

class FirstCollectionViewController: UIViewController {

   var tbvc = CustomTabBar()
   var statisticsData = [Model]()

   let firstCellIdentifier = "FirstCellIdentifier"

   @IBOutlet weak var FirstCollectionView: UICollectionView!

   override func viewDidLoad() {
       super.viewDidLoad()

       FirstCollectionView.delegate = self
       FirstCollectionView.dataSource = self

       FirstCollectionView.register(UINib(nibName: "FirstCollectionViewCell", bundle: nil),forCellWithReuseIdentifier: firstCellIdentifier)
   }
}

这是 UITabBarController 的子类

import UIKit

class CustomTabBar: UITabBarController {

let website = "https:......."

var statisticsData = [Model]()

override func viewDidLoad() {
    super.viewDidLoad()

    let firstTab = FirstCollectionViewController(nibName: "FirstCollectionViewController", bundle: nil)
    let secondTab = SecondCollectionViewController(nibName: "SecondCollectionViewController", bundle: nil)
    let thirdTab = ThirdCollectionViewController(nibName: "ThirdCollectionViewController", bundle: nil)

      viewControllers = [firstTab, secondTab, thirdTab]

    downloadJSON(website: website) {
           firstTab.statisticsData = self.statisticsData
           secondTab.statisticsData = self.statisticsData
           thirdTab.statisticsData = self.statisticsData

           firstTab.FirstCollectionView.reloadData()
           secondTab.SecondCollectionView.reloadData()
           thirdTab.ThirdCollectionView.reloadData()
    }

}

func downloadJSON(website:String, completed:@escaping ()->()){

        guard let qurl = URL(string: website) else { return }

        URLSession.shared.dataTask(with: qurl) { (data, response, error) in
            if error == nil {
                do{

                    self.statisticsData = try JSONDecoder().decode([Model].self, from: data!)

                    DispatchQueue.main.async{
                        completed()
                    }
                } catch {
                    print("JSON Error")
                }}
            }.resume()
   }
}

加载数据后,您应该将数据分配给添加到 tabBarController's 子列表中的 viewControllers,如下所示,

downloadJSON(website: website) {
      firstTab.statisticsData = self.statisticsData
      secondTab.statisticsData = self.statisticsData
      thirdTab.statisticsData = self.statisticsData

      firstTab.FirstCollectionView.reloadData()
      secondTab.SecondCollectionView.reloadData()
      thirdTab.ThirdCollectionView.reloadData()
}

您还可以从 FirstCollectionViewControllerSecondCollectionViewControllerThirdCollectionViewController

viewDidLoad 中删除以下行
tbvc = tabBarController as! CustomTabBar
statisticsData = tbvc.statisticsData