【问题标题】:iPhone - Release viewcontroller after pop operationiPhone - 弹出操作后释放视图控制器
【发布时间】:2011-03-18 11:13:36
【问题描述】:

当我关闭(弹出)视图控制器时,如何释放它以便下次运行时它必须再次初始化?我该如何重新初始化它?

假设我在该视图控制器上放置了一些 cocos2d 游戏层。我想添加一个按钮来关闭它(弹出视图),而不是像现在这样在后台运行。

我只是使用下面的代码来弹出当前视图,但它并没有“杀死”它,只是让它消失并进入根目录。

- (IBAction) closeThisOne {
    [self.navigationController popViewControllerAnimated:YES];
}

我以这种方式创建视图:

- (IBAction) buttonPressed {
if (self.mViewController == nil) {
    MainViewController *vc = [[MainViewController alloc]
                                      initWithNibName:@"MainView" bundle:[NSBundle mainBundle]];
    self.mViewController = vc;
    [vc release];
}
[self.navigationController pushViewController:self.mViewController animated:YES];
}

简单地说..我该怎么做: 根视图 > 开始游戏视图 > 返回根视图(并发布游戏) > 再次开始游戏视图

我仍然有点困惑导航和 cocos2d 的工作原理。提前谢谢!

编辑:好的!我现在看到了......我推动视图控制器的方式我同时保留了它。刚才看到你的帖子比较了,谢谢。

【问题讨论】:

  • 请在创建游戏控制器的位置发布您的代码的 sn-p。

标签: iphone view uiviewcontroller navigation


【解决方案1】:

在调用[self.navigationController pushViewController:vc animated:YES]; 之后,它的保留计数将增加,因为它将被添加到堆栈中。所以在这行代码之后只需要调用[vc release];。当您调用[self.navigationController popViewControllerAnimated:YES]; 时,它将从堆栈中移除,并且其保留计数再次减一。

【讨论】:

    【解决方案2】:

    在将视图控制器推送到 UINavigationController 堆栈后释放它。 UINavigationController 将保留它。

     DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"Nib" bundle:nil];
     // ...
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
    

    【讨论】:

      【解决方案3】:

      当您同时推送 nextClass 对象时,只需释放该类对象,这将释放该 viewController 的对象,而 navigationController 将保留该对象。

      DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"Nib" bundle:nil]; // ... [self.navigationController pushViewController:detailViewController animated:YES]; [detailViewController release];

      当您弹出该视图对象时,导航控制器会自动释放该保留对象。

      【讨论】:

        【解决方案4】:

        据我所知[self.navigationController popViewControllerAnimated:YES]; 将释放当前存在于导航堆栈中的视图控制器。然后你必须重新分配视图控制器。

        【讨论】:

          猜你喜欢
          • 2013-07-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-13
          • 1970-01-01
          • 2023-03-19
          相关资源
          最近更新 更多