Search code examples
iosobjective-cdidselectrowatindexpathstack-unwinding

Use an unwind segue in didSelectRowAtIndexPath?


I have a root view controller A which push segues to a table view controller B. And when a row is selected in B. I want to use an unwind segue to go back to A and pass the text in the row back to the root view A. And then I use the the prepare for segue method to send the stint back like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
     ViewControllerA *vca = (ViewControllerA *)segue.destinationViewController;
    vca.textString = [[_objects objectAtIndex:indexPath.row] objectForKey:@"title"];
}

but what I don't know how to do, is to call the unwind segue in the didSelectRowAtIndexPath method.

Thanks for the help in advance.


Solution

  • Don't call anything. Connect the unwind segue from the cell in your table view in controller B, and the unwind segue will be called by the touch on the cell. You shouldn't need to implement didSelectRowAtIndexPath at a all. You can get the indexPath in prepareForSegue from the sender (Which will be the cell you touched),

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender{
         ViewControllerA *vca = (ViewControllerA *)segue.destinationViewController;
         NSIndexPath *selectedPath = [self.tableView indexPathForCell:sender];
         vca.textString = _objects[selectedPath.row][@"title"];
    }