导航控制器堆栈

发布于 2024-10-14 23:26:44 字数 507 浏览 1 评论 0原文

首先,抱歉我的英语不好。我将尝试解释我的问题:

我有一个 RootViewController (基于导航的项目)。因此,它显示表格视图,当用户选择表格的一行(didSelectRowAtIndexPath)时,您将执行以下操作来显示下一个视图:

NextViewController *nextView = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
[self.navigationController pushViewController:nextView animated:YES];
[nextView release];

如果用户选择导航栏的后退按钮并再次选择该行,然后执行此操作,会发生什么反复?正在创建大量新视图(NextViewController 的实例)(大量内存分配)?或者他只是在堆栈中导航?

你能帮助我吗?我不想以这种方式浪费内存(如果是这样的话)。 谢谢!

First of all, sorry for my bad english. I'll try to explain my question:

I have a RootViewController (Navigation based project). So, it shows the tableview and when the user select a row of the table (didSelectRowAtIndexPath) y do the following to show the next view:

NextViewController *nextView = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
[self.navigationController pushViewController:nextView animated:YES];
[nextView release];

What happens if the user select the back button of the navigationbar and select again the row, and do this repeatedly? A lot of new views (instances of NextViewController) is being created (a lot of memory allocating)? Or is he just navigating the stack?

Can you help me? I dont want to waste memory in that way (if is the case).
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

会傲 2024-10-21 23:26:44

如果用户反复在 RootViewController 和 NextViewController 之间来回切换,则会发生以下情况:

  1. NextViewController 在您的 didSelectRowAtIndexPath: 方法中创建(alloc'd)。因为您在其上调用了 init 方法,所以您有责任释放它。
  2. 您将 nextView 推送到导航控制器堆栈上,导航控制器堆栈将保留它。
  3. 您释放了 nextView,因此唯一带有保留的就是导航控制器。
  4. 一旦用户从 NextViewController 移回,导航控制器就会释放它。现在没有任何内容保留 nextView,因此它会被 dealloc 处理。内存被释放。

基本上,每次用户来回移动时,您都会创建一个 NextViewController (您不是“只是在堆栈中导航”,因为对象每次都会发生变化),但您并没有泄漏大量内容内存量或保留您创建的每个控制器。你这里的内存使用情况很好。

If the user toggles back and forth from your RootViewController to a NextViewController repeatedly, here's what happens:

  1. The NextViewController is created (alloc'd) in your didSelectRowAtIndexPath: method. Because you called an init method on it, you're responsible for releasing it.
  2. You push nextView onto the navigation controller stack, which retains it.
  3. You release nextView, so the only thing with a retain on it is the navigation controller.
  4. Once your user moves back from the NextViewController, the navigation controller releases it. Now nothing is retaining nextView, so it gets dealloc'd. The memory is freed.

Basically, you do create a NextViewController every time your user moves back and forth (you're not "just navigating the stack," since objects are changing each time), but you're not leaking a large amount of memory or holding on to each controller you create. Your memory usage here is fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文