Search code examples
iosuiviewcontrolleruitableviewuistoryboardsegue

How to pass an object from cell to new controller


For each cell in my UITableView I setup a custom object. Like this:

// Reference to the turn
PFObject *turnToPlay = [self.currentUserTurnsToPlay objectAtIndex:indexPath.row];

When a cell is selected I need to pass that object to a new view controller.

This is from my didSelectRowAtIndexPath

if (indexPath.section == 0) {
    [self performSegueWithIdentifier:@"takeGameTurn" sender:self];  
}

I am struggling to find out how I can reference to my PFObject created for the cell, to pass it to the destination view controller. I understand I would pass this into prepareForSegue but how do I get my object from the cell selection.

I have reference to the indexPath from the didSelectRowAtIndexPath but how do I pass that into the segue destination controller? It is like I need another param in that method.


Solution

  • In your prepareForSegue

      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
             if ([segue.identifier isEqualToString:@"takeGameTurn"]){
                   NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
                   PFObject *turnToPlay = [self.currentUserTurnsToPlay objectAtIndex:indexPath.row];
    
              }
        }