Passing values using prepareForSegue

Set Up

The first thing we'll do is create a new single view application. Once the project is loaded, delete your ViewController.swift from your Project Manager as well as from your Storyboard.

Once you have this find a Navigation Controller in your object library and drag it out onto your storyboard, select your Navigation Controller in your StoryBoard and choose "Is Initial View Controller" in your Attributes Inspector. Now that you've got your initial view controller set up let's add a new Cocoa class to your project by right-clicking your main project folder in Project Manager and selecting "New File...". Select, iOS > Source > Cocoa Touch Class and then select next. Create a UITableViewController like I've done here.

We'll assign this new class as our table view in our storyboard by selecting our table view and adding our newly created class in our Identity Inspector it'll look like this.

Before we move on, click on your prototype cell in your storyboard and give it an Identifier of "Cell" and give it a Style of Basic for this example.

Now that we have our main controller lets add a View Controller that we'll send our values to. Drag a View Controller our onto your storyboard just as we did for our Navigation Controller. And just as we did with our FirstTableViewController, we'll add another Cocoa Touch Class, this time uses a UIViewController and we'll name it SecondViewController. Remember to add your new class to your View Controller in your storyboard as we did before for our FirstViewController.

Finally, we'll create a segue by control dragging from our FirstTableViewController to our SecondViewController like so.

Click on your new segue and in your Attributes Inspector add an Identifier, I chose "SendDataSegue" for this example.

We'll also want to add a label to our new View Controller, place it dead center and give it some simple constraints. Finally, create an outlet in your SecondViewController.swift by control dragging from your outlet to your class. 

Name your outlet viaSegueLabel, it should look like this:

@IBOutlet weak var viaSegueLabel: UILabel!


Open your SecondViewController and at the class level, add a property called viaSegue like this:

var viaSegue = ""

Then inside your view controller we'll assign the value of viaSegue to our viaSegueLabel, add the following code to your viewDidLoad:

override func viewDidLoad() {

        viaSegueLabel.text = viaSegue
        // Do any additional setup after loading the view.

We're finished with our viewController, lets move on to our tableViewController.

In our FirstTableViewController lets first quickly set our numberOfSectionsInTableView to 1 and numberOfRowsInSection to 10, I won't go into any detail here:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 10

Now, let's add a method called prepareForSegue, which does exactly what it sounds like it does, it prepares for the upcoming segues.  To learn more about prepareForSegue see Apples Developer Guide. Here's the implementation:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "SendDataSegue" {
            if let destination = segue.destinationViewController as? SecondViewController {
                let path = tableView.indexPathForSelectedRow
                let cell = tableView.cellForRowAtIndexPath(path!)
                destination.viaSegue = (cell?.textLabel?.text!)! 

In this method, we're determining which segue we're preparing for with segue.identifier, we're then using an if let to safely attempt to assign a segue with a destinationViewController with a type of ours. Getting the type lets us model the properties within, thus giving us access to our viaSegue in our SecondViewController, which we take advantage of here.

We haven't actually set up a segue yet, we'll do that now. Lets add a new tableView override function for didSelectRowAtIndexPath, this will allow us to trigger a segue from whichever row we select. The implementation is simple so I'll stop rambling.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        _ = tableView.indexPathForSelectedRow!
        if let _ = tableView.cellForRowAtIndexPath(indexPath) {
            self.performSegueWithIdentifier("SendDataSegue", sender: self)

It's important to note that we are using our segue by it's identifier "SendDataSegue".

Last but not least, lets make sure our cells have some data to pass. Since we used the Basic setup for our prototype cell earlier, we've got access to a textLabel by default, so lets use it:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
        // Configure the cell...
        cell.textLabel?.text = "Hello from cell #\(indexPath.row))"
        return cell

Build and Run

If everything as gone according to my master plan, you should've passed the value you selected. If you're having problems drop a line below or check out the project files.