2012-04-05 40 views
3

我只是在編程時想知道。我有一個rootViewController,它創建了一個childViewController的實例。這個childVC從另一個childViewController(比如childVC2)通過[self.navigationController pushViewController:childVC]推送到我的rootViewController。現在我想知道我的childVC2實例會發生什麼。它會被釋放嗎?因爲當從childVC返回到childVC2時,我創建了一個childVC2的新實例並將其推送到我的rootViewController中。很顯然,我對childVC2的早期實例沒有任何用處,所以發生了什麼與它或郝我可以手動釋放它?將viewController推入另一個viewController後會發生什麼

回答

1

是的,如果你是分配的ViewController你必須釋放它..推會增加其引用計數1,所以你必須確保你彈出它來解碼引用計數..併爲創建例如在.H定義它,創造它的時候做這樣的

if(yourViewController) 
    [yourViewContoller release]; 

yourViewController = [yourViewControllerClass alloc] init]; 

另一個opition是讓它自動釋放,在這種情況下,你是不負責釋放的ViewController

yourViewController = [yourViewControllerClass alloc] init]autorelease]; 
+0

謝謝!但是如果我使用ARC,我的程序會自動爲我做這個工作嗎? – iJatrat 2012-04-05 16:10:34

+0

如果您使用的是ARC,請不要擔心釋放對象。iOS會照顧它。 – 2012-04-05 16:41:30

相關問題