I made a table view with a city list. When I select a city cell, it will pass the city name and show the city's weather forecast by using segue in storyboard. However, every time it seems it will create a new viewcontroller. But I don't want it happens like this. I want it can load only once. For users, when they don't have internet, they still can see the data, and I made a button for them to refresh the data. here is my code:
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]
}
}
I try to use the navigation controller to push the viewcontroller,
weatherViewController=self.storyboard?.instantiateViewController(withIdentifier: "weatherViewController") as! UIViewController
self.navigationController?.pushViewController(weatherViewController!, animated: true)
and in the weatherViewController, I made a custom back button to pop the view controller,
@objc func backAction(){
navigationController?.popViewController(animated: true)
}
However, with using the navigation controller, I don't know how to pass the city name from the selected cell to the weather view controller. How can I resolve the problem? Thanks