0

I create global variable just after import statement:

var kontenid = ""
var judulkonten = ""

then I sent to FreeTiles view controller (other view controller) through tableview and prepareForSegue:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //let cell = tableView.dequeueReusableCellWithIdentifier(reuseContentFreeIdentifier, forIndexPath: indexPath) as! FreeTableViewCell
    var contentku = contents[indexPath.row] as ContentModel
    kontenid = contentku.id
    judulkonten = contentku.title
    performSegueWithIdentifier("lemparkonten", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    if (segue.identifier == "lemparkonten") {
        var svc = segue.destinationViewController as! FreeTiles;
        svc.idcontent = kontenid
        svc.namacontent = judulkonten
    }
}

And on FreeTiles view controller, I put this inside class:

var idcontent :String!
var namacontent :String!

But when I println on FreeTiles view controller:

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

I got two log of idcontent and two log of namacontent, the first is empty and the second filled with correct idcontent and namacontent.

How to avoid get two threw variable result when sent variable between two view controller? What is the correct code to get only one result for each threw variable?

Edited: it seems like cache on xcode log because when I change println, it show only one log.

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

So, I make filter on FreeTiles (second view controller) to get the filled variable.

Regards.

Sarimin
  • 707
  • 1
  • 7
  • 18
  • When do you print your results? Show your code of the FreeTilesViewController. – derdida Jul 02 '15 at 07:11
  • if idcontent.isEmpty && namacontent.isEmpty { //println("Nothing to see here") }else{ var content_id = idcontent var content_name = namacontent println("content_id:\(content_id)") println("content_name:\(content_name)") } – Sarimin Jul 02 '15 at 07:26
  • Where is that code? (ViewDidLoad?) Post your Code of your second ViewController – derdida Jul 02 '15 at 07:27
  • override func viewDidLoad() { super.viewDidLoad() println("konten id nya:\(idcontent)") println("judul nya:\(namacontent)") if idcontent.isEmpty && namacontent.isEmpty { //println("Nothing to see here") }else{ var content_id = idcontent var content_name = namacontent println("content_id:\(content_id)") println("content_name:\(content_name)") }} – Sarimin Jul 02 '15 at 07:29

2 Answers2

0

First of all, if you are declaring global variables then you need to specify it's scope. Let's say:

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

Further, once you have declared global variables you can access it from anywhere and it will also reflect when you will change the value.

So, after assigning values to the variables

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //let cell = tableView.dequeueReusableCellWithIdentifier(reuseContentFreeIdentifier, forIndexPath: indexPath) as! FreeTableViewCell
    var contentku = contents[indexPath.row] as ContentModel
    kontenid = contentku.id
    judulkonten = contentku.title
    performSegueWithIdentifier("lemparkonten", sender: self)
}

In your prepareforsegue method, you don't need to assign the values:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    if (segue.identifier == "lemparkonten") {
        var svc = segue.destinationViewController as! FreeTiles;

    }
}

Now you can access the above global variables anywhere so you can directly use it in other class:

if idcontent.isEmpty && namacontent.isEmpty {
        //println("Nothing to 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 you have any issues against this code.

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
  • I think this throw variable to FreeTiles (second view controller), I tried without this it throw nil : svc.idcontent = kontenid svc.namacontent = judulkonten – Sarimin Jul 02 '15 at 07:49
  • It can't be nil, try to print the value inside tableview itself – Sohil R. Memon Jul 02 '15 at 07:57
  • inside tableview is not nil, but if I do not write svc.idcontent = kontenid and svc.namacontent = judulkonten inside prepareForSegue, it did not sent to FreeTiles (second view controller), so when I run the project it become buggy because the second view controller got nil for idcontent and namacontent. – Sarimin Jul 02 '15 at 08:35
  • @Sarimin As I said you need not to assign the variables, you can directly access from other class, once you have defined as a "public" – Sohil R. Memon Jul 02 '15 at 08:47
0

You are printing your variables 2 times out with your code:

    override func viewDidLoad() { 
       super.viewDidLoad() 
       println("konten id nya:(idcontent)") // first
       println("judul nya:(namacontent)")  // second
       if idcontent.isEmpty && namacontent.isEmpty { //println("Nothing to see here") 
    } else { 
    var content_id = idcontent var content_name = namacontent     
println("content_id: (content_id)") // 3
println("content_name:(content_name)") // 4
    }}

Your Code seems doing well to send data through segues. Otherwise you could check my answer here:

Sending data with Segue with Swift

derdida
  • 14,784
  • 16
  • 90
  • 139