Use an unwind segue in didSelectRowAtIndexPath?
Asked Answered
I

3

5

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.

Ibbie answered 28/5, 2014 at 19:6 Comment(0)
C
15

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"];
}
Covin answered 28/5, 2014 at 20:2 Comment(1)
This technique has the advantage that the code executed can run prior to the segue, whereas didselectRowIndexAtPath runs after the segue has completed. Thanks!Recreate
A
1

SWIFT 4.2 Here is how I am doing it in one of my apps, which opens up a popup menu in as a modal view controller with an embedded table view, users picks data from the table on which the modal view controller closes and show the data from the picked up cell in the calling view controller.

Calling ViewController:

@IBAction func unwindFromPopup(segue: UIStoryboardSegue) {
    if segue.source is MyModalViewController {
        if let senderVC = segue.source as? MyModalViewController {
            MyTextField1.text = senderVC.MyID1InModalVC
            MyTextField2.text = senderVC.MyID2InModalVC
            MyTextField3.text = senderVC.MyID3InModalVC
        }
    }
}

Modal ViewController:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let cell = sender as! UITableViewCell
    let index = cell.convert(CGPoint.zero, to:self.tableView)
    let indexPath = self.tableView.indexPathForRow(at: index)

    // Now that I have the indexPath, I can do my stuff
    let myData = fetchedResultsController.object(at: indexPath!)
    MyID1InModalVC = myData.ID1!
    MyID2InModalVC = myData.ID2!
    MyID3InModalVC = myData.ID3!
}
Aboral answered 30/11, 2018 at 12:6 Comment(0)
G
0

You shouldn't need a prepare for segue if you want to go back to a view controller. You can simply call that view controller directly. Check here.

And then once you sent the parent view controller's property, you can simply pop the current view controller.

[self.navigationController popViewControllerAnimated:yes]

You can do all of this in didSelectRowAtIndexPath.

Gratis answered 28/5, 2014 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.