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

Value of Sender

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

    }

I need to access the String value that a tableviewcell has when the segue is activated. I tried using sender.text but that does not work since the sender is of type AnyObject. Stephen Whitfield Stone Preston

2 Answers

assuming you pass the cell in as the sender when you performTheSegue

self.performSegueWithIdentifier("MySegue", sender:cell)

you can cast the sender parameter as a UITableViewCell (or whatever your subclass is) and then be able to access its properties

override func prepareForSegue(segue: UIStoryboardSegue, sender:AnyObject?){
        let cell = sender as UITableViewCell
        let text = cell.textLabel!.text
    }

where would I put this part

self.performSegueWithIdentifier("MySegue", sender:cell)

Stone Preston

that depends. did you create a segue from the cell to the viewController in your storyboard? or did you create the segue in your storyboard as a manual segue.

if its going from the cell to the viewController then you dont need to call performSegue yourself. if its manual segue going from the viewController to the viewController then you will need to call performSegue in a delegate method such as didSelectRowAtIndexPath

I created a seque from the cell to the viewController in my storyboard. When I simply pasted the second bit of code you provided an error saying that " 'UILabel?' does not have a member named 'text' " Stone Preston

force unwrap the textLabel using

cell.textLabel!.text

again, this is assuming you are using a default UITableViewCell and not a custom subclass. if you have subclasssed and created new properties in the cell textLabel might not be the property you want.

Ok, that works, thanks.