在非常好的书籍“Beginning iPhone Development”(Apress)的第 9 章中,他们解释了如何使用导航控制器和分层表视图构建应用程序。
如果您使用 Instrument/Activity 监视器启动应用程序,应用程序运行良好但有一个大问题:每次从表视图向下钻取到子表时,它会多占用 1Mo 内存!并且这个内存永远不会被释放,当然,最后应用程序会崩溃。对我来说问题来自“RootViewController.h”的以下方法:(
原始源代码是这个ZIP文件的“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];
}
现在,如果你运行应用程序,内存就被很好地释放了!但是,如果您尝试在您已经“访问过”的子表中向下钻取,则应用程序会崩溃。
我们如何才能正确释放内存?
先感谢您。