ios - View doesn't get updated until the second time loaded -


i have main view table view list of countries. when clicking on country name (cell), view loaded via segue passing name of country next view controller's navigation bar title.

the problem on first click title isn't updated, when click button (dismissing current view) , click on country name, second view loads again , shows previous title suppose shown on first attempt.

the code first main view controller:

import uikit  class viewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource {      var sectionsarray = [string]()     var sectionscountries = [array<anyobject>]()      @iboutlet weak var countries: uitableview!      internal func numberofsections(in tableview: uitableview) -> int {         // return number of sections.         return self.sectionsarray.count     }      internal func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {         // return number of rows in section.             return self.sectionscountries[section].count     }      internal func tableview(_ tableview: uitableview, titleforheaderinsection section: int) -> string? {         return self.sectionsarray[section]     }      internal func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {         let cell = tableview.dequeuereusablecell(withidentifier: "countrycell", for: indexpath)         cell.textlabel?.text = self.sectionscountries[indexpath.section][indexpath.row] as? string         return cell     }      var valuetopass:string!      internal func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) {         print("you selected cell #\(indexpath.row)!")          // cell label         let indexpath = tableview.indexpathforselectedrow;         let currentcell = tableview.cellforrow(at: indexpath!) uitableviewcell!;          valuetopass = currentcell?.textlabel?.text         performsegue(withidentifier: "cellsegue", sender: self)         //print(valuetopass)     }      override func prepare(for segue: uistoryboardsegue, sender: any?) {         if segue.identifier == "cellsegue" {             let destination = segue.destination as! countryviewcontroller             destination.passedvalue = valuetopass         }     }      override func viewdidload() {          super.viewdidload()         // additional setup after loading view, typically nib.         let url = url(string: "http://cyber7.co.il/swift/countries/countries-list.json")!         let task = urlsession.shared.datatask(with: url) { (data, response, error) in             if error != nil {                 print(error)             } else {                 if let urlcontent = data {                     {                         let jsonresult = try jsonserialization.jsonobject(with: urlcontent, options: jsonserialization.readingoptions.mutablecontainers)                         result in jsonresult as! [dictionary<string, anyobject>]{                             self.sectionsarray.append(result["sectionname"] as! string)                             self.sectionscountries.append(result["sectioncountries"] as! array<string> [anyobject])                         }                     } catch {                         print("json processing failed")                     }                     dispatchqueue.main.async(execute: { () -> void in                         self.countries.reloaddata()                     })                 }             }         }         task.resume()     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }   } 

the code second view controller:

import uikit  class countryviewcontroller: uiviewcontroller {      var passedvalue:string!      @iboutlet weak var navbar: uinavigationbar!      @ibaction func backbutton(_ sender: anyobject) {         self.dismiss(animated: true, completion: nil)     }      override func viewwillappear(_ animated: bool) {         self.navbar.topitem?.title = passedvalue     }      override func viewdidload() {         super.viewdidload()         // additional setup after loading view.     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }  } 

when have segue set table view cell view controller in storyboard performed automatically when cell selected. call perform segue in cell selection method performing segue second time after first 1 has been performed.

remove tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) method , data passing logic in prepareforsegue:

override func prepare(for segue: uistoryboardsegue, sender: any?) {     if segue.identifier == "cellsegue" {         let destination = segue.destination as! countryviewcontroller         let indexpath = countries.indexpathforselectedrow         let currentcell = countries.cellforrow(at: indexpath!)         destination.passedvalue = currentcell?.textlabel?.text     } } 

Comments