gpt4 book ai didi

ios - 我可以只加载一次 View Controller 吗?

转载 作者:可可西里 更新时间:2023-11-01 01:57:05 25 4
gpt4 key购买 nike

我制作了一个带有城市列表的表格 View 。当我选择一个城市单元格时,它将传递城市名称并通过在 Storyboard 中使用 segue 显示城市的天气预报。但是,每次它似乎都会创建一个新的 View Controller 。但我不希望事情就这样发生。我希望它只能加载一次。对于用户来说,当他们没有网络时,他们仍然可以看到数据,我做了一个按钮让他们刷新数据。这是我的代码:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showCurrentWeather", sender: nil)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showCurrentWeather"{
let dvc = segue.destination as? WeatherViewController
let selectedIndexPath = self.tableView.indexPathForSelectedRow
if let selectedRow = selectedIndexPath?.row{
dvc?.infoFromViewOne=cityName[selectedRow]

}
}

我尝试使用导航 Controller 来推送 View Controller ,

   weatherViewController=self.storyboard?.instantiateViewController(withIdentifier: "weatherViewController") as! UIViewController

self.navigationController?.pushViewController(weatherViewController!, animated: true)

在 weatherViewController 中,我制作了一个自定义后退按钮来弹出 View Controller ,

    @objc func backAction(){

navigationController?.popViewController(animated: true)
}

但是,通过使用导航 Controller ,我不知道如何将城市名称从选定的单元格传递到天气 View Controller 。我该如何解决这个问题?谢谢

最佳答案

您需要将其转换为 as! WeatherViewController 不是 作为! UIViewController 因为它是默认返回可选的 UIViewController

weatherViewController = self.storyboard?.instantiateViewController(withIdentifier: "weatherViewController")    as! WeatherViewController
weatherViewController.infoFromViewOne = cityName[indexPath.row]
self.navigationController?.pushViewController(weatherViewController, animated: true)

在 VC 不可见时将其保留在内存中也不是一个好主意,您必须按原样进行创建/取消

关于ios - 我可以只加载一次 View Controller 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51547758/

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