2010-09-11 73 views
1

我正在創建一個加載屏幕UIView,它被添加到子視圖中,而某些XML從某個URL被解析。一旦XML返回,加載屏幕將從其超級視圖中移除。何時發佈從超級視圖中移除的UIView

我的問題是我該如何釋放這個對象?

在下面的代碼中,您會看到我將removeFromSuperview發送到加載屏幕,但我仍然擁有此對象的所有權,除非我將其釋放。但是,如果我釋放它,那麼viewdidUnloaddealloc中將沒有任何內容可以發佈。

- (void)loadView { 
    ... 
    loadingScreen = [[LoadingScreen alloc] initWithFrame: self.view.frame]; 
    [self.view addSubview:loadingScreen]; //retain count = 2 
} 

-(void)doneParsing { 
    ... 
    [loadingScreen removeFromSuperview]; //retain count = 1 
    [loadingScreen release]; //should i release the loading screen here? 
} 

- (void)viewDidUnload { 
    [loadingScreen release]; //if viewDidUnload is called AFTER doneParsing, this 
          //will cause an exception, but the app might crash before 
          //doneParsing is called, so i need something here 
} 

- (void)dealloc { 
    [loadingScreen release]; //if i've already released the object, i can't release here 
} 
+0

其實我認爲我已經通過在作爲子視圖添加之後釋放loadingScreen來解決此問題,然後從doneParsing中的superview中移除loadingScreen。我沒有在viewDidUnload或dealloc中釋放loadingScreen。 – dianovich 2010-09-11 14:13:16

+1

這是正確的。把它給予超級視圖,然後釋放它。之後,這不再是你的責任,你可以忘掉它。當超級視圖被釋放時,超級視圖將釋放它。 – 2010-09-11 15:14:03

回答

2

當您發佈loadingScreen時,將其重置爲零值。

[loadingScreen release]; 
loadingScreen = nil; 

[nil release]不會發生任何事情。

相關問題