iOS通过Swift中的Segue传输多个JSON文件数据

问题描述

我正在尝试制作一个国家天气预报应用,我有vc1和vc2。

每个国家/地区都存在要在vc2中解析的JSON文件,当单击vc1的表格视图单元格时,我们尝试在vc2中实现对该国家/地区的JSON文件的解析。

但是,我不知道如何通过segue将vc1的JSON文件名传递给vc2。

使用segue从vc1传递到vc2时,变量为nil。有什么解决办法吗?

感谢阅读。

vc1

class MainViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    var countries = [Countries]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        
        let jsonDecoder = JSONDecoder()
        guard let dataAsset = NSDataAsset(name: "countries")
            else {
                return
        }
        do {
            countries = try jsonDecoder.decode([Countries].self,from: dataAsset.data)
        } catch {
            print(error.localizedDescription)
        }
        tableView.reloadData()
    }
    
    
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return countries.count
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell",for: indexPath)
        let country: Countries = countries[indexPath.row]
        cell.imageView?.image = UIImage(named: "flag_" + country.asset_name)
        cell.textLabel?.text = country.korean_name
        return cell
    }
    
    // Data Transfer
    override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
        guard let nextViewController: SecondViewController = segue.destination as? SecondViewController else {
            return
        }
        guard let cell: UITableViewCell = sender as? UITableViewCell else {
            return
        }
        func name(indexPath: IndexPath) {
            let country: Countries = countries[indexPath.row]
            nextViewController.title = cell.textLabel?.text
            nextViewController.secondAssetName = country.asset_name
        }
        
    }
}

vc2

class SecondViewController: UIViewController,UITableViewDataSource {
    
    
    var weathers = [Weather]()
    var secondAssetName: String?
        
    @IBOutlet weak var tableView: UITableView!
    
        override func viewDidLoad() {
        super.viewDidLoad()
            
            self.tableView.delegate = self
            self.tableView.dataSource = self
        
            
        let jsonDecoder = JSONDecoder()
            guard let dataAsset = NSDataAsset(name: secondAssetName ?? " ") else {
            return
        }
        do {
            weathers = try jsonDecoder.decode([Weather].self,numberOfRowsInSection section: Int) -> Int {
        return weathers.count
    }

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: CustomTableViewCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell",for: indexPath) as! CustomTableViewCell
        let weather: Weather = weathers[indexPath.row]
        
        switch weather.state {
            case 10:
                cell.cellImageView?.image = UIImage(named: "sunny.png")
            case 11:
                cell.cellImageView?.image = UIImage(named: "cloudy.png")
            case 12:
                cell.cellImageView?.image = UIImage(named: "rainy.png")
            case 13:
                cell.cellImageView?.image = UIImage(named: "snowy.png")
            default:
                return cell
        }
        cell.cityNameLabel.text = weather.city_name
        cell.temperatureLabel.text = String(weather.celsius)
        cell.rainfallProbabilityLabel.text = String(weather.rainfall_probability)
        return cell
    }
}

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)