【问题标题】:UINavigationController with two copies of the same view controller on stackUINavigationController 在堆栈上具有相同视图控制器的两个副本
【发布时间】:2012-02-22 19:38:49
【问题描述】:

我正在使用 UINavigationController 将 UIViewController 推送到堆栈上,然后当用户在表格视图中选择一行时,我分配了一个新版本的视图控制器并将其推送到堆栈上,这很好,但是当我开始按下后退按钮,我用来存储信息的 NSMutableArray 似乎保留了刚刚从堆栈中弹出的视图控制器的值。

这是将新的压入堆栈的方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section == 1) {
        if (hasLoadedResponses && [responses count] == 0) {
            return;
        }

        VideoDetailViewController_iPhone *nextView = [[VideoDetailViewController_iPhone alloc] initWithNibName:@"VideoDetailViewController_iPhone" bundle:nil withVideoArray:[responses objectAtIndex:indexPath.row]];
        nextView.navController = navController;
        [navController pushViewController:nextView animated:YES];
        [nextView release];
    }

}

我确定我错过了一些愚蠢的东西,但现在似乎找不到。

【问题讨论】:

  • VideoDetailViewController_iPhone 如何与NSMutableArray 交互?这可能就是您的问题所在。
  • 而且,可变数组是如何以及在哪里定义的?如果它是一个类变量而不是一个属性,那可能就是问题所在。

标签: iphone objective-c ios uiviewcontroller uinavigationcontroller


【解决方案1】:

假设可变数组是 [responses objectAtIndex:indexPath.row] 因为是您传递给新推送的视图控制器的唯一值,那么如果您正在修改它,您必须考虑到它是同一个对象,所以修改是共享的。

你可以写:

. withVideoArray:[NSMutableArray arrayWithArray:[responses objectAtIndex:indexPath.row]];

正如我所说,我假设 object 是您的问题的可变数组。

另一个更聪明的解决方案是将用于存储可变数组的属性定义为“复制”而不是“保留”。

【讨论】:

  • 感谢您的回复,虽然这不是我的问题,但它确实教会了我一些东西。原来我的问题是 NSNotificationCenter,当我进入下一个视图时我没有移除观察者,所以数据被覆盖了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多