2011-09-15 110 views
0

我用兩個視圖控制器編程創建一個標籤欄,如下面的代碼。當我不再需要標籤欄時,我發現很難清理內存。關於發佈標籤欄控制器,Apple的文檔非常有限。我不知道如何釋放'viewControllers'數組中的所有視圖控制器。我試圖打印出保留計數,發現X &Ÿ的retainCount更是高達5如何釋放UITabBarController及其所有視圖控制器?

@interface X:UIViewController 
@interface Y:UIViewController 

@interface Z: UIViewController { 
    UITabBarController *tabBar; 
} 
@end 

@implementation Z 
-(IBAction)openTabBarUp{ 
    UITabBarController *tabBar = [[UITabBarController alloc] init]; 

    X *x = [[X alloc] init]; 
    Y *y = [[Y alloc] init]; 

    tabBar.viewControllers = [NSArray arrayWithObjects: x, y, nil]; 
    [self.view addSubView: tabBar.view]; 

} 

這是我嘗試釋放內存:

-(IBAction)removeTabBar{ 
    [tabBar.view removeFromSuperView]; 
    [tabBar release]; 
    tabBar = nil; 
} 

感謝

利奧

+0

對象的絕對retainCount毫無意義。你在浪費你的時間思考, – bbum

回答

1
-(IBAction)openTabBarUp{ 
    tabBar = [[UITabBarController alloc] init]; 

    X *x = [[X alloc] init]; 
    Y *y = [[Y alloc] init]; 

    tabBar.viewControllers = [NSArray arrayWithObjects: x, y, nil]; 
    [self.view addSubView: tabBar.view]; 

} 

您完成需要UITabBarController *tabBar = [[UITabBarController alloc] init];openTabBarUp方法,因爲您已經在頭文件中聲明瞭它的一個實例。您可以使用[tabBar release];來釋放tabBar,但Apple堅持將tabBarController添加爲主窗口的rootview,而不是任何視圖控制器的一部分。

UPDATE

Apple reference documents on UITabBarController狀態

當部署一個標籤欄的界面,您必須安裝此視圖作爲 根你的窗口。與其他視圖控制器不同,一個標籤欄 絕不應該被安裝爲另一個視圖控制器的子視圖。

+0

我明白了。我想這就是爲什麼我得到奇怪的保留計數。非常感謝。我需要考慮替代UITabBar – leo

+0

由於您查看了保留計數,您會得到奇怪的保留計數!嚴重 - 對象的絕對保留數是沒有意義的。 – bbum

相關問題