75

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")]
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
patrikbelis
  • 1,350
  • 2
  • 16
  • 35
  • possible duplicate of [Pass variables from one ViewController to another in Swift](http://stackoverflow.com/questions/24044108/pass-variables-from-one-viewcontroller-to-another-in-swift) – Paulw11 Oct 05 '14 at 23:05

3 Answers3

147

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

iJeep
  • 989
  • 1
  • 10
  • 28
Sasha Reid
  • 1,547
  • 1
  • 10
  • 8
  • sasha. check on my answer. I need to like. If will be clicked on CELL1 it will open link 1, If will be clicked on CELL2 it will open link2 – patrikbelis Oct 22 '14 at 20:23
  • 5
    I tried the 2) approach and I always get an exception telling that "Receiver PlayerTableViewController has no segue with identifier 'playerSegue'". Just calling `performSegueWithIdentifier` works fine, but then no data is transfered to the new view. :[ – althaus Nov 26 '14 at 11:09
  • @althaus Have you made sure there is a segue named "playerSegue" in your storyboard? This image shows what you should look for. If that fails, send me some screenshots and I'll check it out. https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/art/SB_H_set_segue_identifier_2x.png – Sasha Reid Nov 27 '14 at 11:50
  • @SashaReid I worked around this by moving the logic to @prepareForSegue@, but after working some more with Xcode (new to the stuff), I guess I just had the direction of the segue wrong. The name was definitely ok. – althaus Nov 28 '14 at 08:09
  • 2
    @althaus @SashaReid isn't better use `indexPathForSelectedRow` instead of `didSelectRowAtIndexPath` callback? Example: `destinationVC.programVar = products[self.tableView.indexPathForSelectedRow!.row]` in the `prepareForSegue` method. Let me know if I am wrong (I am new to the stuff too). – Paulo Henrique Nonaka Apr 04 '16 at 14:50
  • 31
    @SashaReid I'm having the same issue as the one althuas mentioned in a previous comment. I'm attempting to do the 2nd method and am slightly confused. The segue is going from HomeTableVC to PlayerTableVC in the example, so why are we using destinationVC.performSegueWithIdentifier("playerSegue", sender: self) ? Isn't the current VC the one that's performing the segue? – FortuneFaded Apr 17 '16 at 22:05
  • 2
    I think the last line which calls .performSegueWithIdentifier() should be invoked on self instead of destinationVc – Herno Jan 31 '19 at 02:48
57

With Swift 3 & 4

In The First ViewController (Send The Value)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "MainToTimer") {
        let vc = segue.destination as! YourViewController
        vc.verificationId = "Your Data"
    }
}

In the second viewController (Catch The Value)

var verificationId = String()
DragonFire
  • 3,722
  • 2
  • 38
  • 51
Imtee
  • 1,345
  • 13
  • 14
6

If you have no need to discern the action by the identifier but only by the target class...

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = segue.destination as? YourViewController {
        vc.var_name = "Your Data"
    }
}
Andrea Leganza
  • 447
  • 7
  • 7