Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Josh Page
Josh Page
1,928 Points

Help: Swift Segue with Variables is not working!

Hi, I am trying to build a Detail View controller from scratch in swift. I have succesfully built the table, built the two views, added data to the tables and created a segue. But when I try to pass a variable between the two screens using my segue it will not work. The segue can successfully pass strings BUT not Variables

The code I am using is: //Segue override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "DetailSegue" { var svc = segue.destinationViewController as DetailViewController; svc.tooPass = performer } }

//Function Run on Tap.
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    var performer = tableData[indexPath.row]
    performSegueWithIdentifier("DetailSegue", sender: self)  
}

1 Answer

jean-marie castellucci
jean-marie castellucci
4,954 Points

You don't have to use didSelectRow, try this :

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "DetailSegue" { 
    let svc = segue.destinationViewController as DetailViewController

    let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()!

    svc.tooPass = tableData[indexPath.row] } }