释放UINavigationController内部的ViewControllers表单内存 [英] Releasing ViewControllers form memory inside a UINavigationController

查看:145
本文介绍了释放UINavigationController内部的ViewControllers表单内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UINavigationController构建应用.我有一个ViewController,它显示了一些内容,当它收到用户的交互(例如,点击按钮)时,将显示一个新的ViewController.

I'm building an app using a UINavigationController. I have a ViewController that shows some content, and when it receives interaction from the user (tapping a button for example) a new ViewController is displayed.

这是代码:

FirstViewController.m

-(IBAction)goToSecondVC:(id)sender{  
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController: secondVC];    
}

我想在SecondViewController出现后从内存中释放FirstViewController.然后例如,如果我从SecondViewController跳到FifthViewController,我也希望发布SecondViewController. 当我在控制器之间来回跳转时,应用程序消耗的内存增加了,这意味着它们没有被释放.有什么想法吗?

I want to release FirstViewController from memory once SecondViewController has appeared. And then for example, if I jump from SecondViewController to FifthViewController I'd like SecondViewController to be released as well. The memory consumed by the app increases when I jump back and forth between controllers, meaning that they are not being released. Any thoughts?

当我在控制器之间切换时,内存指示器会发生这种情况. ARC已激活:

This is what happens with the memory indicator when I switch between controllers. ARC is activated:

推荐答案

UINavigationController使用堆栈数据结构来管理viewControllers,因此您有2个操作:PopPush. firstVC --push-> secondVC --push-> thirdVC.现在,第一,第二和第三将保留在内存中,以便更快地弹出.如果您认为这会占用大量内存,而您无法为viewController使用其他演示文稿,例如以模态方式呈现它,或将其添加为已编译的viewController,然后手动将其删除,但使用UINavigationController,则仅执行pop操作将确保viewController将被释放(如果没有其他强大的引用).

UINavigationController uses stack data structure to mange viewControllers, so you have 2 operations: Pop and Push. firstVC --push-> secondVC --push-> thirdVC. now first, second and third will stay in memory for faster popping. If you think that this consume a lot of memory than you can use other presentations for your viewController, for example modally presenting it, or adding it as a chiled viewController and then remove it manually, but with UINavigationController, only pop operation will ensure that the viewController will be released (if no other strong references on it).

这篇关于释放UINavigationController内部的ViewControllers表单内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆