导航控制器堆栈
首先,抱歉我的英语不好。我将尝试解释我的问题:
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果用户反复在 RootViewController 和 NextViewController 之间来回切换,则会发生以下情况:
didSelectRowAtIndexPath:
方法中创建(alloc
'd)。因为您在其上调用了init
方法,所以您有责任释放它。nextView
推送到导航控制器堆栈上,导航控制器堆栈将保留它。nextView
,因此唯一带有保留的就是导航控制器。nextView
,因此它会被dealloc
处理。内存被释放。基本上,每次用户来回移动时,您都会创建一个 NextViewController (您不是“只是在堆栈中导航”,因为对象每次都会发生变化),但您并没有泄漏大量内容内存量或保留您创建的每个控制器。你这里的内存使用情况很好。
If the user toggles back and forth from your RootViewController to a NextViewController repeatedly, here's what happens:
alloc
'd) in yourdidSelectRowAtIndexPath:
method. Because you called aninit
method on it, you're responsible for releasing it.nextView
onto the navigation controller stack, which retains it.nextView
, so the only thing with a retain on it is the navigation controller.nextView
, so it getsdealloc
'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.