Pass data through segue

patrikbelis picture patrikbelis · Oct 6, 2014 · Viewed 96.5k times · Source

I'm doing simple iOS application with tableview controller and detailView. All I want is to pass data through segue.

this is how it looks like

this is how it looks.

All I want is that u click on "Markíza" it will open URL video number 1 and if u click on "TV JOJ" it will open URL video number 2 in player.

My tableview cells:

    struct Program {
        let category : String
        let name : String
    }


   var programy = [Program]()
        self.programy = [Program(category: "Slovenské", name: "Markíza"),
                         Program(category: "Slovenské", name: "TV JOJ")]

Answer

Sasha Reid picture Sasha Reid · Oct 6, 2014

Swift works exactly the same way as Obj-C but is reworked in the new language. I don't have a lot of information from your post but let's give a name to each TableViewController to help with my explanation.

HomeTableViewController (this is the screenshot you have above)

PlayerTableViewController (this is the player screen you want to travel to)

With that said, in PlayerTableViewController you need to have a variable that will store the passed data. Just under your class declaration have something like this (if you intend to store the struct as a single object rather than the array:

class PlayerTableViewController: UITableViewController {

    var programVar : Program?

    //the rest of the class methods....

After that there are two ways you can send data to the new TableViewController.

1) Using prepareForSegue

At the bottom of HomeTableViewController you will use the prepareForSegue methods to pass the data. Here is an example of the code you'll use:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    // Create a variable that you want to send
    var newProgramVar = Program(category: "Some", name: "Text")

    // Create a new variable to store the instance of PlayerTableViewController 
    let destinationVC = segue.destinationViewController as PlayerTableViewController
    destinationVC.programVar = newProgramVar
    }
}

Once PlayerTableViewController has loaded the variable will be already set and usable

2) Using didSelectRowAtIndexPath

If specific data needs to be sent based on which cell is selected you can use didSelectRowAtIndexPath. For this to work, you need to give your segue a name in the storyboard view (let me know if you need to know how to do this too).

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    // Create a variable that you want to send based on the destination view controller 
    // You can get a reference to the data by using indexPath shown below
    let selectedProgram = programy[indexPath.row]

    // Create an instance of PlayerTableViewController and pass the variable
    let destinationVC = PlayerTableViewController()
    destinationVC.programVar = selectedProgram

    // Let's assume that the segue name is called playerSegue
    // This will perform the segue and pre-load the variable for you to use
    destinationVC.performSegueWithIdentifier("playerSegue", sender: self)
}

Let me know if you need any other info on this