我有一个源视图控制器和一个目标视图控制器。 我想在移动到此屏幕后显示从UI(目标)中的网址获取的数据("显示" segue)。
为了做到这一点,在源VC中我使用"准备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()}
} }
我无法想出一个合适的解决方案。
答案 0 :(得分:0)
您需要更新主线程中的UI内容,因为您正在执行后端调用: -
DispatchQueue.main.async({
destinationVc.tabelView.reloadData()
})
答案 1 :(得分:0)
你可以做两件事之一。
转换到目标屏幕并让该控制器在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()
}
}