Get double variable when sent variable to next viewcontroller swift -


i create global variable after import statement:

var kontenid = "" var judulkonten = "" 

then sent freetiles view controller (other view controller) through tableview , prepareforsegue:

func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) {     //let cell = tableview.dequeuereusablecellwithidentifier(reusecontentfreeidentifier, forindexpath: indexpath) as! freetableviewcell     var contentku = contents[indexpath.row] contentmodel     kontenid = contentku.id     judulkonten = contentku.title     performseguewithidentifier("lemparkonten", sender: self) }  override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     // new view controller using segue.destinationviewcontroller.     // pass selected object new view controller.     if (segue.identifier == "lemparkonten") {         var svc = segue.destinationviewcontroller as! freetiles;         svc.idcontent = kontenid         svc.namacontent = judulkonten     } } 

and on freetiles view controller, put inside class:

var idcontent :string! var namacontent :string! 

but when println on freetiles view controller:

println("konten id nya:\(idcontent)") println("judul nya:\(namacontent)") 

i got 2 log of idcontent , 2 log of namacontent, first empty , second filled correct idcontent , namacontent.

how avoid 2 threw variable result when sent variable between 2 view controller? correct code 1 result each threw variable?

edited: seems cache on xcode log because when change println, show 1 log.

if idcontent.isempty && namacontent.isempty {         //println("nothing see here")     }else{         var content_id = idcontent         var content_name = namacontent         println("content_id:\(content_id)")         println("content_name:\(content_name)")     } 

so, make filter on freetiles (second view controller) filled variable.

regards.

first of all, if declaring global variables need specify it's scope. let's say:

public var kontenid = "" public var judulkonten = "" 

further, once have declared global variables can access anywhere , reflect when change value.

so, after assigning values variables

func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) {     //let cell = tableview.dequeuereusablecellwithidentifier(reusecontentfreeidentifier, forindexpath: indexpath) as! freetableviewcell     var contentku = contents[indexpath.row] contentmodel     kontenid = contentku.id     judulkonten = contentku.title     performseguewithidentifier("lemparkonten", sender: self) } 

in prepareforsegue method, don't need assign values:

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     // new view controller using segue.destinationviewcontroller.     // pass selected object new view controller.     if (segue.identifier == "lemparkonten") {         var svc = segue.destinationviewcontroller as! freetiles;      } } 

now can access above global variables anywhere can directly use in other class:

if idcontent.isempty && namacontent.isempty {         //println("nothing see here")     }else{         var content_id = kontenid         var content_name = judulkonten         println("content_id:\(content_id)")         println("content_name:\(content_name)")     } } 

let me know, if have issues against code.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -