Ios UIViewController在解除时不接收dealloc或viewDidUnload
更新:我在调试中犯了一个错误-这个问题与此无关-请参阅下面的评论。 注意:我使用的是自动参考计数 当我的应用程序启动时-我使用Ios UIViewController在解除时不接收dealloc或viewDidUnload,ios,ios5,uiviewcontroller,uikit,automatic-ref-counting,Ios,Ios5,Uiviewcontroller,Uikit,Automatic Ref Counting,更新:我在调试中犯了一个错误-这个问题与此无关-请参阅下面的评论。 注意:我使用的是自动参考计数 当我的应用程序启动时-我使用presentViewController:animated:completion在UINavigationController中显示视图控制器。该视图控制器将第二个视图控制器加载到导航堆栈中。第二个视图控制器使用[self.presentingViewController dismissViewControllerAnimated:YES completion:nil]
presentViewController:animated:completion
在UINavigationController
中显示视图控制器。该视图控制器将第二个视图控制器加载到导航堆栈中。第二个视图控制器使用[self.presentingViewController dismissViewControllerAnimated:YES completion:nil]
自行关闭。我的问题是,在第一个视图控制器中既不调用dealloc也不调用viewDidUnload。但是,对于instruments,我可以看到,一旦显示的视图控制器被取消,视图控制器就不再被分配。显示第一个视图控制器的代码是
- (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。任何帮助都很好。他们不会被呼叫,因为您没有正确释放视图控制器 您可以使用
alloc
分配settingsVC
和navVC
,从而获得对这两者的所有引用,您必须在以后发布它们,而您没有这样做
您可以这样做:
- (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];
}
}
另一种选择是立即对这两种方法进行自动释放 我使用的是自动参考计数-仍然是这样吗?很抱歉,我想我的问题中包括了这个。我对它进行了更新以反映这一点。当然,ARC在这里会有所不同。。。那么,在这种情况下,我不知道发生了什么,对不起。你能告诉我我的假设是应该调用dealloc还是viewDidUnload吗?这意味着我对内存管理和UIViewControllers的理解是正确的吗?是的,一旦相应的视图控制器被解除,这两个都应该被调用。我是个白痴。我将要测试的dealoc方法放在了错误的视图控制器中。所以我的dealloc被调用,这很有意义。