ios - Swift: String value not passing from tableView to Other view -
what i'm trying capture textlabel.text of cell in table view controller, , pass label in view. after lot of research here's have far:
var valuetopass:string! override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { println("you selected cell #\(indexpath.row)!") // cell label let indexpath = tableview.indexpathforselectedrow(); let currentcell = tableview.cellforrowatindexpath(indexpath!) uitableviewcell!; valuetopass = currentcell.textlabel!.text println("\(valuetopass) captured") performseguewithidentifier("thesegue", sender: self) } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if (segue.identifier == "thesegue") { var viewcontroller = segue.destinationviewcontroller finalview viewcontroller.resto = valuetopass } }
and here code finalview, variable 'resto' lives:
class finalview: uiviewcontroller{ var resto:string! override func viewdidload() { super.viewdidload() println("the restoname is: \(resto)") restolabel.text = resto } @iboutlet var restolabel: uilabel! }
but when check whether worked correctly, result of println statements:
the restoname is: nil selected cell #13! tanad thai captured
why string 'tanad thai' not passing on 'rest' in finalview class? appreciate can get.
you have done excellent job instrumenting code, need think have learned. @ output of instrumentation:
the restoname is: nil selected cell #13! tanad thai captured
that output proves finalview's viewdidload
being called before table view controller's didselectrowatindexpath:
called. thus, cannot use didselectrowatindexpath:
set value on viewdidload
depends! need find earlier moment. personally, suggest doing work in prepareforsegue
.
also, i'm little worried may performing segue twice - once in storyboard , once in code. if have set segue emanate cell in storyboard, not need call performseguewithidentifier
- called automatically.
Comments
Post a Comment