使用展开segue推送数据

时间:2015-01-04 04:20:01

标签: ios xcode

我有一个正在使用的解开segue,然后我准备我用来推送数据的segue。 我需要展开segue来推送数据,但我遇到了组合它们的问题。 这是unwind segue代码 -

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

这是准备segue代码 -

- (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];
    }
}

以下是我尝试解开segue但不推送数据的内容。

- (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];
    }
}

1 个答案:

答案 0 :(得分:1)

你的问题相当不完整,所以我只能根据假设回答......

首先,此方法应位于您将返回的视图控制器中。

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

}

其次,使用您的prepareForSegue:方法,而不是您的unwindFromDetailViewController:方法(属于第一个视图控制器),从第二个视图控制器传递数据。我相信虽然您在if ([segue.identifier isEqualToString:@"showRecipeDetail"])语句中使用了前向segue的标识符而不是unwind segue的标识符,但它因此返回false,因此prepareForSegue:方法中的整个块都没有执行一点都不(这一行:[self dismissViewControllerAnimated:YES completion:nil];是完全不必要的,因为只要所有内容都正确连接,自动展开就会发生。)所以现在尝试删除条件,看看数据是否按预期传递,例如:

- (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;
}

如果要重新添加指定检查segue标识符的条件,则必须专门为展开 segue设置标识符。