2010-10-30 101 views
0

我試圖顯示一個包含NavigationController的模式視圖控制器。不過,我無法弄清楚在哪裏發佈控制器。通常情況下,我只是在顯示控制器後才能釋放控制器,但這不起作用;據推測這與導航控制器有關。任何幫助將是偉大的!這是有問題的代碼:被創建,並用作「RootViewController的」Modal View Controller中使用UINavigationController進行內存管理

-(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. 
} 

回答

0

「控制器」 - 但從來沒有真正顯示出來。因此,雖然它通常會被展示它的人保留 - 沒有人做到這一點。

我有點困惑,爲什麼你這樣做 - 但我猜這是你的「控制器」,這是導致問題的釋放。

你可以在技術上解決這個問題,直到modalNavController消失爲止 - 但我不知道你爲什麼要這麼做。

+1

控制器正在由導航控制器(modalNavController)顯示。她應該釋放它。出現這個錯誤是因爲他既autoreleases'modalNavController'以及特別釋放它(在他的評論部分)。這是造成這次事故的原因。 – 2010-10-30 18:49:56

+0

你是對的! – Brad 2010-10-30 18:58:01

+0

謝謝!如果可能的話,我從不自動發佈任何東西,所以我沒有注意到我不小心把它放在那裏!我本來可以找幾個小時(我想我*已*實際上已經找了幾個小時,但是......)並沒有看到! – nosirrahcd 2010-10-31 00:18:41

1

你正在自動發佈modalNavController以及專門釋放它,這是什麼導致它過早dealloc。自動發佈或專門發佈,但儘量不要同時進行。

所以:

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]; 
相關問題