【发布时间】:2010-10-19 16:45:53
【问题描述】:
在非常好的书籍“iPhone 开发入门”(Apress)的第 9 章中,他们解释了如何使用导航控制器和分层表视图构建应用程序。
如果您使用 Instrument/Activity 监视器启动应用程序,应用程序运行良好但有一个大问题:每次从表视图向下钻取到子表时,它会多占用 1Mo 内存!并且这个内存永远不会被释放,当然,最后应用程序会崩溃。
对我来说,问题来自“RootViewController.h”的以下方法:
(原始源代码是这个ZIP file的“09 Nav”)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
SecondLevelViewController *nextController = [self.controllers objectAtIndex:row];
NavAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navController pushViewController:nextController animated:YES];
}
在这个方法中,“nextcontroller”永远不会被释放。为了使用命令[nextController release];我做了以下修改:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
SecondLevelViewController *nextController = [[SecondLevelViewController alloc] init ];
nextController = [self.controllers objectAtIndex:row];
NavAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navController pushViewController:nextController animated:YES];
[nextController release];
}
现在,如果您运行该应用程序,内存就会很好地释放!但是,如果您尝试在您已经“访问过”的子表中向下钻取,应用程序就会崩溃。
我们怎样才能正确的释放内存呢?
提前谢谢你。
【问题讨论】:
标签: ios uitableview memory-management