gpt4 book ai didi

iOS Swift - 数据获取速度不够快,无法到达另一个 UIViewController

转载 作者:行者123 更新时间:2023-11-30 12:16:13 26 4
gpt4 key购买 nike

我有一个源 View Controller 和一个目标 View Controller 。我想在移动到此屏幕后立即显示从 UI(目标)中的 url 获取的数据(“显示”segue)。

为了做到这一点,在源 VC 中,我使用“prepare for segue”方法,其中我调用一个函数,该函数返回一个数组,其中包含我想要显示的所有获取的数据,并将其传递到要显示的目标 VC在 UITableView 中。问题是很多时候并没有从 url 中获取全部数据,因此我将一个空数组传递给目标。

这是源VC中的代码:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVc = segue.destination as?
ShowCharacterInfoViewController{

fetchCharacterArrayFromUrl(characterName: cellContent){ results in

destinationVc.array.append(contentsOf:results)
destinationVc.tabelView.reloadData()}
} }

我想不出合适的解决方案。

最佳答案

您可以执行以下两件事之一。

  1. 加载数据并仅在函数返回数据后执行 segue。
  2. 转换到目标屏幕并让该 Controller 在 viewWillAppear 中加载数据。

    // Add a place to save the results
    var saveResults: MyResultsType?


    @IBAction func someButtonAction(_ sender: Any) {

    // Consider putting a "Loading..." dialog here

    fetchCharacterArrayFromUrl(characterName: cellContent){ results in
    self.saveResults = results
    DispatchQueue.main.async({
    // Hide the "Loading..." dialog
    performSegue(withIdentifier: "ID of your Segue", sender: sender) //
    })
    }
    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destinationVc = segue.destination as? ShowCharacterInfoViewController {
    // I'm not sure append() is really what you need here
    destinationVc.array.append(contentsOf: savedResults)
    destinationVc.tabelView.reloadData()
    }
    }

关于iOS Swift - 数据获取速度不够快,无法到达另一个 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45405539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com