更新:我在调试中犯了一个错误 - 这个问题不是相关的 - 请参阅下面的评论。
注意:我使用的是自动参考计数
当我的应用开始时 - 我使用UINavigationController
在presentViewController:animated:completion
内展示了一个视图控制器。该视图控制器将第二个视图控制器加载到导航堆栈。第二个视图控制器使用[self.presentingViewController dismissViewControllerAnimated:YES completion:nil]
来解除自身。我的问题是,在第一个视图控制器中都没有调用dealloc和viewDidUnload。但是,使用乐器,我可以看到一旦所呈现的视图控制器被解除,视图控制器就不再被分配。显示第一个视图控制器的代码是
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// check if our context has any accounts
if( [self.accounts count] == 0 )
{
// Display the Add Account View Controller
MySettingsViewController *settingsVC = [[MySettingsViewController alloc] initWithNibName:@"MySettingsViewController" bundle:nil];
settingsVC.appContext = self.appContext;
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:settingsVC];
navVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
// Display the Add Account View Controller
}
else
{
navVC.modalPresentationStyle = UIModalPresentationFormSheet;
}
[self presentViewController:navVC animated:YES completion:nil];
}
}
所以,我没有引用应该坚持的settingsVC,但我不知道为什么我的dealloc没有被调用。任何帮助都会很棒。
答案 0 :(得分:0)
由于您未正确释放视图控制器,因此无法调用它们。
您使用settingsVC
分配navVC
和alloc
,从而获得对您必须在以后发布但尚未发布的两个内容的引用。
你可以这样做:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// check if our context has any accounts
if( [self.accounts count] == 0 )
{
// Display the Add Account View Controller
MySettingsViewController *settingsVC = [[MySettingsViewController alloc] initWithNibName:@"MySettingsViewController" bundle:nil];
settingsVC.appContext = self.appContext;
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:settingsVC];
navVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
// At this points, "settingsVC" is additionally retained by the navigation controller,
// so we can release it now.
[settingsVC release];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
// Display the Add Account View Controller
}
else
{
navVC.modalPresentationStyle = UIModalPresentationFormSheet;
}
[self presentViewController:navVC animated:YES completion:nil];
// At this point "navVC" is retained by UIKit, so we can release it as well.
[navVC release];
}
}
另一种选择就是autorelease
。