iphone - 模态视图 Controller 内使用 UINavigationController 进行内存管理

标签 iphone memory-management ios

我正在尝试显示包含导航 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/

相关文章:

iphone - 要求用户在 iPhone 崩溃后发送崩溃日志

java - Enum#values() 是否在每次调用时分配内存?

iphone - UIWebView - 无法在文本字段中显示当前 URL

ios - 如何给静态库一个版本号?

iphone - 在 iPhone 的 TextView 中显示数组值

iphone - 是否可以通过名称而不是索引以编程方式在受控段中选择一个选项?

iphone - UITableView cellForRowAtIndexPath 问题

iphone - 在 Instruments 中分析 iPhone 应用程序时缺少符号名称

c++ - 如果 _heapchk() 返回 "okay",我可以假设堆没有损坏吗?

c# - 如何开发我自己的 .NET 控件?