Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use didSelectRowAtIndexPath or prepareForSegue method for UITableView?

I'm using storyboards and I have a UITableView. I have a segue setup that pushes from my table to the detail VC. But which method should I use to handle this? I'll have to pass a couple objects to the detail view. But do I use didSelectRowAtIndex or -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender?

like image 486
Jon Avatar asked Nov 15 '11 01:11

Jon


1 Answers

If you use prepareForSegue:sender:then you won't have as much to change if you later decide to trigger the segue from some control outside the table view.

The prepareForSegue:sender: message is sent to the current view controller, so I'd suggest something like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {     // Assume self.view is the table view     NSIndexPath *path = [self.tableView indexPathForSelectedRow];     DetailObject *detail = [self detailForIndexPath:path];     [segue.destinationViewController setDetail:detail]; } 

In Swift:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {     let path = self.tableView.indexPathForSelectedRow()!     segue.destinationViewController.detail = self.detailForIndexPath(path) } 
like image 156
rob mayoff Avatar answered Oct 03 '22 20:10

rob mayoff