0

I have an unwind segue that i am using, and then i have a prepare for segue that i am using to push data. I need the unwind segue to push the data but i am having issues combining them. Here is the unwind segue code -

- (IBAction)unwindFromDetailViewController:(UIStoryboardSegue *)segue {
    // ViewController *detailViewController = [segue sourceViewController];
    NSLog(@"%@", segue.identifier);
}

And here is the prepare for segue code -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
        NSIndexPath *indexPath = nil;
        Recipe *recipe = nil;
        if (self.searchDisplayController.active) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            recipe = [searchResults objectAtIndex:indexPath.row];
        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            recipe = [recipes objectAtIndex:indexPath.row];
        }

        PersonDetailTVC *destViewController = segue.destinationViewController;
        destViewController.recipe = recipe;

        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

And here is what i tried which is unwinding the segue, but not pushing the data.

- (IBAction)unwindFromDetailViewController:(UIStoryboardSegue *)segue {
    if ([segue.identifier isEqualToString:@"CustomTableCell"]) {
        NSIndexPath *indexPath = nil;
        Recipe *recipe = nil;
        if (self.searchDisplayController.active) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            recipe = [searchResults objectAtIndex:indexPath.row];
        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            recipe = [recipes objectAtIndex:indexPath.row];
        }       

        PersonDetailTVC *destViewController = segue.destinationViewController;
        destViewController.recipe = recipe;

        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
matt suff
  • 27
  • 1
  • 5
  • 1
    You keep reposting the same question, but I already directed you to this post with many answers about the same exact topic -- "can i return data when i unwind a segue?" http://stackoverflow.com/q/13038622/2274694 . I suggest you check it out. – Lyndsey Scott Jan 04 '15 at 04:27
  • And if your question's not being answered, instead of reposting the same exact question 3 times, I suggest you work on it to make it clearer. – Lyndsey Scott Jan 04 '15 at 04:29
  • 1
    Did you specifically set an identifier for your *unwind segue*? I suspect that you're simply using your *segue* identifier and that your `if ([segue.identifier isEqualToString:@"showRecipeDetail"])` in `prepareForSegue` is returning false and thus the block isn't executing at all. (This line: `[self dismissViewControllerAnimated:YES completion:nil];` is complete unnecessary.) – Lyndsey Scott Jan 04 '15 at 05:02

1 Answers1

1

Your question's rather incomplete so I can only answer based on assumptions...

First off, this method should be in the view controller you're returning to.

- (IBAction)unwindFromDetailViewController:(UIStoryboardSegue *)segue {

}

Secondly, use your prepareForSegue: method, not your unwindFromDetailViewController: method (which belongs in the first view controller), to pass your data from that second view controller. I believe though that you're using your forward segue's identifier and not your unwind segue's identifier in your if ([segue.identifier isEqualToString:@"showRecipeDetail"]) statement, that it's therefore returning false, and thus that entire block in your prepareForSegue: method isn't executing at all. (This line: [self dismissViewControllerAnimated:YES completion:nil]; is complete unnecessary since the unwind occurs automatically as long as everything's hooked up correctly.) So try removing the conditional for now just to see if the data is passed as expected, ex:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    NSIndexPath *indexPath = nil;
    Recipe *recipe = nil;
    if (self.searchDisplayController.active) {
        indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
        recipe = [searchResults objectAtIndex:indexPath.row];
    } else {
        indexPath = [self.tableView indexPathForSelectedRow];
        recipe = [recipes objectAtIndex:indexPath.row];
    }

    PersonDetailTVC *destViewController = segue.destinationViewController;
    destViewController.recipe = recipe;
}

If you want to re-add a conditional specifying a check for your segue's identifier, you have to set an identifier specifically for the unwind segue.

Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • I tried the prepareforsegue that you suggested but it is still not passing the info with the unwind segue, i dont know if im just not understanding what you are saying or what. But it still does nothing. – matt suff Jan 04 '15 at 17:05
  • Like I said though, your question's incomplete, for example: Is `unwindFromDetailViewController:` in the view controller you're unwinding to? Are you sure your segue's set up properly? Are you sure `prepareForeSegue` is being called? Have you put in a breakpoint or nslog to check? And how are you determining that the data hasn't been passed? Your need to provide way more info... – Lyndsey Scott Jan 04 '15 at 17:10
  • I have the unwind segue in the view controller i am unwind ing to and prepareforsegue in the view controller i am unwinding from, but without a push segue how am i supposed to pass the information from the second to the first with the unwind?\ – matt suff Jan 04 '15 at 17:14
  • @mattsuff The unwinding is automatic. (I've tested it myself. And everything works for me. Passing data et al.) – Lyndsey Scott Jan 04 '15 at 17:16
  • @mattsuff I'd recommend updating your question (not to change your original code since that would be confusing for future answer seekers), but to add the code you're using in your PersonDetailTVC to check that new data passed back from the other view controller. – Lyndsey Scott Jan 04 '15 at 17:20