我正在尝试显示包含导航 Controller 的模态视图 Controller 。但我不知道在哪里释放 Controller 。通常,我会在显示 Controller 后释放它,但这在这里不起作用;据推测这与导航 Controller 有关。任何帮助都会很棒!这是有问题的代码:
-(IBAction)displayCreateModifyExerciseViewController:(id)sender {
CreateModifyExerciseViewController *controller = [[CreateModifyExerciseViewController alloc] initWithNibName:@"CreateModifyExerciseView"
bundle:nil];
controller.delegate = self;
controller.title = @"Create Exercise";
UINavigationController *modalNavController = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
modalNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save"
style:UIBarButtonItemStyleDone
target:controller
action:@selector(done:)];
[self presentModalViewController:modalNavController animated:YES];
//I want to say [controller release];
// [modalNavController release];
//But that causes a crash because controller ends up dealloc-ing.
}
最佳答案
您正在自动释放 modalNavController 以及专门释放它,这就是导致它过早释放的原因。要么自动释放,要么专门释放,但尽量不要同时进行。
所以:
CreateModifyExerciseViewController *controller = [[[CreateModifyExerciseViewController alloc] initWithNibName:@"CreateModifyExerciseView" bundle:nil] autorelease];
controller.delegate = self;
controller.title = @"Create Exercise";
UINavigationController *modalNavController = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease]; // <-- you originally autorelease here
modalNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Save"
style:UIBarButtonItemStyleDone
target:controller
action:@selector(done:)] autorelease]; // <-- this was leaking in your code -- needs to be autoreleased
[self presentModalViewController:modalNavController animated:YES];
// Don't release now because everything was autoreleased
或者专门发布所有内容:
CreateModifyExerciseViewController *controller = [[CreateModifyExerciseViewController alloc] initWithNibName:@"CreateModifyExerciseView" bundle:nil];
controller.delegate = self;
controller.title = @"Create Exercise";
UINavigationController *modalNavController = [[UINavigationController alloc] initWithRootViewController:controller]; // <-- you originally autorelease here
modalNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Save"
style:UIBarButtonItemStyleDone
target:controller
action:@selector(done:)] autorelease]; // <-- this was leaking in your code -- needs to be autoreleased
[self presentModalViewController:modalNavController animated:YES];
// Now we specifically release the controllers because the call to -presentModalViewController:animated: owns them
[controller release];
[modalNavController release];
关于iphone - 模态视图 Controller 内使用 UINavigationController 进行内存管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4060089/