2

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

Penny Chen
  • 79
  • 1
  • 10

1 Answers1

1

You need to cast it as! WeatherViewController not as! UIViewController as it's the default return is optional UIViewController

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

Also keeping a VC in memory when it's not visible isn't a good idea you have to follow create/deinit as it's

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • thanks for answering my question, I have tried before,I cast it as! WeatherViewcontroller, but it always said "Value of type 'UIViewController?' has no member 'infoFromViewOne'" – Penny Chen Jul 26 '18 at 21:39
  • where you declare weatherViewController ??? it may be declared **var weatherViewController:UIViewController!** while it should be **var weatherViewController:WeatherViewController!** – Shehata Gamal Jul 26 '18 at 21:44
  • I declare as an instance variable. I make a little mistake, but now I can see the infoFromViewOne. however, when I tested it, it still cannot keep the data when I turn off the internet when pressing the cell again. – Penny Chen Jul 26 '18 at 22:19
  • update: now can keep the data, but only one city! press cityA,show CityA,then pressed other cityB,C,D,... only show CityA again. is that because of my structure design wrong? – Penny Chen Jul 26 '18 at 22:29