ios - Swift passing data between views -


i working on swift app , i've run weird issue. i'm quite new swift , ios development think may not doing correct way.

i have uitableview , able read contents of selected cell without issue:

override func tableview(_ tableview: uitableview, didselectrowat  indexpath: indexpath) {     let cell = self.tableview.cellforrow(at: indexpath)     selecteditem = (cell?.textlabel?.text)! } 

i have variable set @ top of class so:

var selecteditem: string = string() 

i use prepare seque seque new page/view , pass in data selected:

override func prepare(for segue: uistoryboardsegue, sender: any?) {     if segue.identifier == "showdetails"     {         if let destinationvc = segue.destination as? itemdetails {             destinationvc.detailstodisplay = selecteditem         }     } } 

the problem while data passed between views , appears in ui, it's not recent data. example when run code , select item, first time, nothing appears, if click button, select item again, time appear in next view. , on subsequent selections in ui, navigates next view doesn't update text until pick item.

in mind it's selecteditem variable not being set on each selection expect , passed through, instead on subsequent selections in ui, updated.

how pass actual correct item each time , not previous 1 or none @ (which happens on first run).

thanks!

your prepare segue called before didselectrow @ index

change code below

override func prepare(for segue: uistoryboardsegue, sender: any?) {     if segue.identifier == "showdetails"     {         if let destinationvc = segue.destination as? itemdetails {    let cell = tableview.cellforrowatindexpath(self.tableview.indexpathforselectedrow!) uitableviewcell  destinationvc.detailstodisplay = (cell.textlabel?.text)!          }     } } 

Comments