0

我已经搜索了很多,但仍然找不到这个问题的答案......

我正在使用情节提要在 xcode 5.0 中开发 iphone 应用程序(用于大学)。

我有一个带有表格视图的视图控制器,一切正常(数据源,委托......)。项目存储在一个数组中(称为“array1”)。我有一个添加按钮,它会显示我想要添加(如果选中)到 array1 的项目列表。我将选中的项目存储在另一个数组(“array2”)中。问题是,当我将 array2 传递给前一个视图控制器时,我会丢失 array1 中的所有数据(它变为 nil)。我用来将数据从 array2 传递到 VC 的代码是:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"updateFavoritesSegue"]) {
    FavoritesViewController *targetVC = (FavoritesViewController*) segue.destinationViewController;
    [targetVC updateFavorites:[self newFavoritesArray]];
    }
}

updateFavorites 方法的实现如下。

-(void) updateFavorites:(NSArray *)newFavorites {
    [self.favorites addObjectsFromArray:newFavorites];
    [self.favoritesTableView reloadData];
}

非常感谢您的帮助。

4

1 回答 1

2

你为什么不使用一些处理程序?

第二个VC.h

@property (nonatomic, copy) void (^didFinishPickingItemsHandler)(NSArray *items);

然后从第一个VC:

    - (void)showSecondScreen {
        MYSecondVC *secondVC = /* initialisation code here */
        __weak MyFirstVC *self_ = self;
        secondVC.didFinishPickingItemsHandler = ^(NSArray *items) {
           /* update you data here with your array1 and received picked items */
           [self.navigationController popViewControllerAnimated:YES];
        };
        [self.navigationController pushViewController:secondVC animated:YES];
    }
于 2013-09-29T15:55:59.343 回答