基本上我有一个具有介绍性 View 的应用程序。一旦到达某个 View ,之前的 View 将完全无法访问,因此我想将它们从堆栈中移除并释放它们消耗的所有内存。做这个的最好方式是什么?现在我正在做类似的事情
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
NSArray *allControllersCopy2 = [allViewControllers copy];
for (id object in allControllersCopy2) {
if([allControllersCopy2 indexOfObject:object] == ([allControllersCopy2 count] - 1)){
NSLog(@"IGNORE CURRENT VIEW");
}
else{
[allViewControllers removeObject:object];
[object release];
}
}
self.navigationController.viewControllers = allViewControllers;
[allControllersCopy2 release];
这真的释放了这些 View 消耗的内存吗?还是只是从堆栈数组中删除 View ?
最佳答案
当您准备好推送“特定 View ”时,请使用 setViewControllers:animated: 将堆栈替换为最终的 ViewController,而不是使用 pushViewController 将其添加到堆栈。
假设 controller = 'that certain view'...
不要做 [self.navigation pushViewController:controller animated:YES],做:
[self.navigationController setViewControllers:[NSArray arrayWithObject:controller] animated:YES];
这将释放所有以前的 View Controller ,它们的 dealloc 方法将被调用,内存释放等
关于iphone - 释放导航 Controller 堆栈中特定 UIVIewController 的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12080783/