在呈现包含UITableViewController作为模态视图的UINavigationController时,我遇到内存管理问题。我自动释放UINavigationController和UITableViewController,但是当我打开然后在使用Allocations Instrument时关闭模态视图时,视图创建的内存在关闭时不会被释放。
- (void)showOptions {
OptionsViewController *optionsViewController = [[[OptionsViewController alloc] initWithNibName:@"OptionsViewController" bundle:nil] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:optionsViewController] autorelease];
navController.navigationBar.tintColor = [UIColor lightGrayColor];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
navController.modalPresentationStyle = UIModalPresentationFormSheet;
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissOptions) name:@"dismissOptions" object:nil];
[[(ApplicationDelegate *)[[UIApplication sharedApplication] delegate] viewController] presentModalViewController:navController animated:YES];
}
- (void)dismissOptions {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"dismissOptions" object:nil];
[[(ApplicationDelegate *)[[UIApplication sharedApplication] delegate] viewController] dismissModalViewControllerAnimated:YES];
}
感谢您的时间。
答案 0 :(得分:0)
首先,何时/何地发布了“dismissOptions”通知?是发布了吗?你的代码中的自我指向的是什么?它收到通知吗?也许最重要的是,这个通知解决了什么问题?
模态视图控制器会自动保留,因此这样的代码就足够了:
- (void)showOptions {
OptionsViewController *optionsViewController = [[OptionsViewController alloc] initWithNibName:@"OptionsViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:optionsViewController];
[self presentModalViewController:navController animated:YES];
[optionsViewController release];
[navController release];
}
然后在OptionsViewController或任何其他可能会关闭模态视图的视图控制器中:
[self dismissModalViewControllerAnimated:YES];
(注意在这里使用自足,作为对dismissModalViewControllerAnimated的调用气泡直到最初呈现模态视图控制器的视图控制器)