tableview - Passing values between ViewControllers based on list selection in Swift -
i'm trying pass selected index number of listview selection 1 viewcontroller running issue tableview didselectrowatindexpath delegate runs later prepareforsegue function.
basically, in didselectrowatindexpath, seta variable, picked in prepareforsegue. issue prepareforsegue seems run second cell selected , before didselectrowatindexpath function called variable not passed.
my main bits of code are:
tableview didselectrowatindexpath delegate, sets 'selectedresult'...
func tableview(tableview: uitableview!, didselectrowatindexpath indexpath: nsindexpath!) { selectedresult = indexpath.item txtnamesearch.resignfirstresponder() //get rid of keyboard when table touched. println("saved result") //tableview.deselectrowatindexpath(indexpath, animated: false) //var tappeditem: todoitem = self.todoitems.objectatindex(indexpath.row) todoitem //tappeditem.completed = !tappeditem.completed //tableview.reloaddata() }
prepareforsegue function sends variable 'topass' other viewcontroller ('detailviewcontroller'):
override func prepareforsegue(segue: uistoryboardsegue!, sender: anyobject!){ if (segue.identifier == "detailseque") { println("preparing") var svc = segue!.destinationviewcontroller detailviewcontroller svc.topass = selectedresult //println(tableview.indexpathsforselectedrows(). //svc.topass = tableview.indexpathforselectedrow() } }
thanks in advance
if have outlet tableview in viewcontroller
, can call indexpathforselectedrow
in prepareforsegue
.
if viewcontroller
subclass of uitableviewcontroller
, can do:
let row = self.tableview.indexpathforselectedrow().row println("row \(row) selected")
if viewcontroller
not subclass of uitableviewcontroller
, set iboutlet
uitableview
in view controller:
@iboutlet var tableview: uitableview!
and wire in interface builder , call prepareforsegue
show above.
Comments
Post a Comment