2012-02-10 51 views
0

更新:我在調試時犯了一個錯誤 - 此問題不是相關的 - 請參閱下面的註釋。UIViewController在解除時未收到dealloc或viewDidUnload

注:我使用的自動引用計數

當我的應用程序啓動 - 我目前有presentViewController:animated:completion一個UINavigationController內部的視圖控制器。該視圖控制器將第二個視圖控制器加載到導航堆棧上。第二個視圖控制器使用[self.presentingViewController dismissViewControllerAnimated:YES completion:nil]來解散它自己。我的問題是,在第一個視圖控制器中都不會調用dealloc和viewDidUnload。但是,通過儀器,我可以看到一旦所提交的視圖控制器被解僱,視圖控制器就不再分配。呈現的第一個視圖控制器的代碼是

- (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不會被調用。任何幫助都會很棒。

+0

我是個白癡。我把dealloc方法放在錯誤的視圖控制器中進行測試。所以我的dealloc被調用,這是有道理的。 – Brian 2012-02-10 16:09:23

回答

0

他們沒有被調用,因爲你沒有正確地釋放你的視圖控制器。

你分配既settingsVCnavVCalloc從而得到擁有這兩個,你以後必須釋放你沒有做引用。

你可以這樣說:

- (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]; 
    }                                                           
}   

的選擇將是到autorelease都馬上。

+0

我正在使用自動引用計數 - 這仍然是這種情況?對不起 - 關於我的問題,我想我已經包括了。我已經更新它來反映這一點。 – Brian 2012-02-10 15:50:06

+0

當然ARC在這裏有所作爲......在那種情況下,我不知道發生了什麼,對不起。 – DarkDust 2012-02-10 15:53:45

+0

你能告訴我,如果我的假設,無論是dealloc或viewDidUnload應該被調用 - 意思是我對內存管理的理解和UIViewControllers正確? – Brian 2012-02-10 15:55:09

相關問題