ios - 使用 instantiateViewControllerWithIdentifier 时从后台内存中删除 UIViewController 的正确方法是什么?

标签 ios objective-c uiviewcontroller

我正在展示一个像这样的 View Controller :

UINavigationController * PlacesNC = [storyboard instantiateViewControllerWithIdentifier:@"PlacesNavigationController"];

[self presentViewController:PlacesNC animated:YES completion:nil];

当我到达使用导航 View Controller 呈现的最终 View Controller 时,如何从内存中删除初始 View Controller ?

编辑

在我的应用程序中,可以用这种方式呈现多个 UIViewController。我将尝试展示我的堆栈的样子。

____ modal vc 5 * current view controller
____ modal vc 4
____ modal vc 3
____ modal vc 2
____ modal vc 1
____ root view controller

所以如果我有这样的堆栈,我如何删除 vc 1、2、3 甚至 4 以便我可以释放这些其他 View Controller 正在使用的内存?允许这样的堆栈可能不正确,但这是我现在拥有的,我只需要临时修复,因为 iPhone 4 无法处理这样的堆栈中使用的内存量。因此,在我想出另一种方式来呈现我的 View Controller 之前,我只需要能够从堆栈中删除其中的一些。

最佳答案

你不能,你也不想。它仍然在 View Controller 层次结构中,正如我在此图中所示(显示了 DrillViewController,但 RootViewController 仍然存在,在它“后面”):

enter image description here

呈现 View Controller 不会破坏呈现的 View Controller ,也不应该;如果是这样,您将无法关闭呈现的 View Controller 并发现呈现的 View Controller 仍然坐在那里。

此外,在 iOS 8 中,完全可以呈现一个 View Controller 并将呈现的 View Controller 的 View 显示在呈现 View Controller 的 View 前面,而呈现 View Controller 的 View 在它后面仍然可见。例如,这就是 UIAlertController 演示文稿的工作方式。显然,如果呈现 View Controller 在那种情况下不复存在,那将是一场灾难。

现在,您真正的意思可能是:我的 View Controller 有一个使用大量内存的属性。当我的 View Controller 不在最前面时,我不需要坚持下去。所以我想在展示另一个 View Controller 时释放它。在这种情况下,只需手动管理内存:在 viewDidDisappear: 上将该属性设置为 nil,并(以某种方式)在 viewWillAppear: 上恢复其值当呈现的 View Controller 被关闭时。

最后一个建议:也许真正的问题是呈现的 View Controller 只是错误的层次结构类型。也许您真正想要做的是用新的 View Controller 替换您在 View Controller 中的原始 View Controller - 因为您从不回到它并且不需要它不再。这是完全可能的,但是您当然需要设置不同的层次结构。

关于ios - 使用 instantiateViewControllerWithIdentifier 时从后台内存中删除 UIViewController 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30035854/

相关文章:

objective-c - 关闭 NSWindow 而不激活下面的窗口

iphone - 使用单步旋转时如何实现流畅的动画/如何在旋转开始时获取新的帧大小?

ios - Apple Mach-O 链接器 (Id) 错误 .. "__objc_empty_cache",需要框架引用

objective-c - 如何用零填充左侧的整数?

ios - Facebook 测试推送通知 "Device token does not match the environment"

ios - NSNotification 被调用了两次

objective-c - UITableViewCell 中的 MKMapView

ios - 跟踪用户当前位置后的 Google map myLocation 按钮颜色 - ios

ios - 如何使 View Controller 从左侧显示并且不填充整个 View ?

ios - 在 ViewController 之间传递 bool 值