I have a table view that when a cell is selected it pushes a view controller onto the navigation stack:
SAPostTableViewController *postViewController = [[SAPostTableViewController alloc] initWithNibName:NSStringFromClass([SAPostTableViewController class]) bundle:nil];
postViewController.site = site;
[self.navigationController pushViewController:postViewController animated:YES];
[postViewController release];
SAPpostTableViewController 有一个静态的 tableView,它的单元格是从一个 nib 加载的。
我已经覆盖了initWithNibName:bundle:
方法:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.sections = [NSMutableDictionary dictionary];
}
return self;
}
sections
是保留财产。
在我有这个viewDidLoad
:SAPostTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cellVisibiltyChanged:) name:@"SAStaticCellVisibiltyChanged" object:nil];
}
所以要匹配viewDidUnload
:
- (void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"SAStaticCellVisibiltyChanged" object:nil];
}
但是,当我按下导航栏中的后退按钮(所有标准行为,无覆盖)并SAPostTableViewController
弹出时,它不会调用viewDidUnload
或dealloc
. 所以这意味着,如果我然后重新选择推动SAPostTableViewController
它的单元格,则会创建一个新实例SAPostTableViewController
并前后重复,这意味着内存使用量不断增加,因为弹出SAPostTableViewController
的 s 永远不会被释放。(我通过在分配上运行 Instruments 知道这一点)
奇怪的是,如果我两次发布 SAPostTableViewController,它会按我的预期工作:
SAPostTableViewController *postViewController = [[SAPostTableViewController alloc] initWithNibName:NSStringFromClass([SAPostTableViewController class]) bundle:nil];
postViewController.site = site;
[self.navigationController pushViewController:postViewController animated:YES];
[postViewController release];
[postViewController release];
(如果我添加第三个发布声明,它会像我预期的那样崩溃,只有 2 个)
我已经使用retainCount
并逐步执行了在上面直接代码的第一行中执行的代码行,retainCount 保持为 1。它在第一行和第二行之间跳跃,所以我看不到它在任何地方被保留额外的时间?
SAPpostTableViewController 只用在这个地方,它不是任何东西的委托,也没有委托。
我怎样才能找到解决方法,还是我错过了一些简单的东西?
以下是 Instruments 在推送一次 SAPostTableViewController 后显示的内容(只有一个发布声明):
以及反复来回导航后显示的内容(再次,一个发布声明):