2012-02-22 70 views
5

作爲我的應用程序啓動的一部分,我循環了幾個視圖控制器。一切正常,但視圖控制器不釋放。 (viewDidUnload和dealloc永遠不會被調用)。每次更改狀態之前,我都會關閉所有呈現的視圖控制器。他們被解僱,並viewWillDisappear被每視圖控制器:dismissModalViewControllerAnimated不釋放視圖控制器

- (void)stateChange:(NSString *)state { 

// Dismiss any exiting modals/popups 
[self dismissPopup]; 
[self dismissModalViewControllerAnimated: NO];   

if([state isEqualToString:@"StateBoot"]) { 

    CLRemotePrimaryBootViewController * viewControllerBoot = [[CLRemotePrimaryBootViewController alloc]initWithNibName: @"CLRemoteBootView" bundle:nil]; 
    [self presentModalViewController:viewControllerBoot animated:NO]; 
    [viewControllerBoot release]; 
} 

else if([state isEqualToString:@"StateLogin"]) { 

    CLRemotePrimaryAuthViewController *viewControllerAuth = [[CLRemotePrimaryAuthViewController alloc]initWithNibName: @"CLRemoteAuthView" bundle:nil]; 
    [self presentModalViewController:viewControllerAuth animated:NO]; 
    [viewControllerAuth release]; 
} 

else if([state isEqualToString:@"StateMain"]) { 

    [self setSelectedIndex:0]; 
} 

else if([state isEqualToString:@"StateStop"]) { 

    // TBD 
} 

}

解僱後,self.presentedViewController實際上是零。關於如何強制iOS發佈這些未使用的視圖控制器的任何想法?

編輯 -

已解決!希望這篇文章能夠幫助其他人 - 我很困惑。事實證明,我的模態視圖控制器是從init方法中設置觀察者(NSNotificationCenter)的基類派生的。在觀察視圖控制器可以被釋放之前,這些觀察者必須被移除。在我的情況下,這變得更加困難,因爲觀察者是塊而不是選擇器。當添加這些觀察員,和id返回爲每一個 - 所以我不得不跟蹤這些在一個NSMutableArray,並添加釋放觀察員在我的基類中的方法:

+ (void) safeUnsubscribe:(id)object { 

if (object != nil) { 

    if ([object isKindOfClass:[CLRemotePrimaryBaseViewController class]]) { 

     CLRemotePrimaryBaseViewController* viewController = (CLRemotePrimaryBaseViewController*)object; 
     [viewController unsubscribe]; 
    }   
} 

} `

` - (無效)手柄:(的NSString *)名稱usingBlock:(CLObjBlock)嵌段{

id observer = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:nil usingBlock:^(NSNotification * notification){ block([notification object]); }]; 
[self.observers addObject:observer];  

}`

// Must call before dealloc or listener will never be released!

' - (無效){退訂

for (id observer in self.observers) { 

    [[NSNotificationCenter defaultCenter] removeObserver:observer]; 
} 

[self.observers removeAllObjects]; 

[[NSNotificationCenter defaultCenter] removeObserver:self]; 

}

+0

你應該張貼修復作爲回答你自己的問題,然後接受它。我不清楚,你發現一個修復,直到挖掘你的問題。 – kevlar 2012-07-14 23:55:15

回答

1

已解決!希望這篇文章能夠幫助其他人 - 我很困惑。事實證明,我的模態視圖控制器是從init方法中設置觀察者(NSNotificationCenter)的基類派生的。在觀察視圖控制器可以被釋放之前,這些觀察者必須被移除。在我的情況下,這變得更加困難,因爲觀察者是塊而不是選擇器。當添加這些觀察員,和id返回爲每一個 - 所以我必須保持在一個NSMutableArray跟蹤這些,並添加釋放觀察員在我的基類中的方法:

+ (void) safeUnsubscribe:(id)object { 

if (object != nil) { 
    if ([object isKindOfClass:[CLRemotePrimaryBaseViewController class]]) { 

     CLRemotePrimaryBaseViewController* viewController = (CLRemotePrimaryBaseViewController*)object; 
     [viewController unsubscribe]; 
    }   
} 

} 



- (void)handle:(NSString *)name usingBlock:(CLObjBlock)block { 

id observer = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:nil usingBlock:^(NSNotification * notification){ block([notification object]); }]; 
[self.observers addObject:observer];  

} 


// Must call before dealloc or listener will never be released! 

- (void)unsubscribe { 

for (id observer in self.observers) { 

    [[NSNotificationCenter defaultCenter] removeObserver:observer]; 
} 

[self.observers removeAllObjects]; 

[[NSNotificationCenter defaultCenter] removeObserver:self]; 

} 
0

內,您的視圖控制器是還沒有被釋放的對象。爲每個視圖控制器定義dealloc方法(記得調用[super dealloc])並放置一個斷點以確定是否被調用。

通過每個視圖控制器查看已分配的任何對象並確保它已被釋放。

相關問題