我怎麼能這樣做,當選項卡被選中,當前的一個被卸載,並且下一個被加載,所以一次只加載一個?或者我應該不這樣做?我知道如何使用普通的UIViewController作爲根VC,但不確定使用UITabBarController。另外,有沒有辦法讓一個標籤到另一個標籤的過渡動畫?任何幫助?謝謝!!iPhone Dev - 懶加載一個標籤欄應用程序
編輯:...如果我卸載視圖控制器,那麼他們的標籤欄上的圖標都不見了...也許我就卸下了自己的看法..
我怎麼能這樣做,當選項卡被選中,當前的一個被卸載,並且下一個被加載,所以一次只加載一個?或者我應該不這樣做?我知道如何使用普通的UIViewController作爲根VC,但不確定使用UITabBarController。另外,有沒有辦法讓一個標籤到另一個標籤的過渡動畫?任何幫助?謝謝!!iPhone Dev - 懶加載一個標籤欄應用程序
編輯:...如果我卸載視圖控制器,那麼他們的標籤欄上的圖標都不見了...也許我就卸下了自己的看法..
你不能真正管理的UITabBarController unfortunaly所以你不能做懶加載。你可以通過管理你自己的TabBar,但你說你已經知道,
來管理你自己的標籤欄,儘管你要做的就是在ViewController中設置一個帶有TabBarItems的UITabBar,然後實現TabBar委託協議,主要是 - 的TabBar:didSelectItem:方法基礎上,項目編號,你可以加載新的視圖控制器和釋放任何其他 所以每當tabbarItem選擇改變其被調用,那麼:編輯:這一代碼進入在UIViewController
-(void)addTabBar{
NSMutableArray* items=[[NSMutableArray alloc] init];
UITabBarItem *eventsItem= [[UITabBarItem alloc] initWithTitle:@"Events" image:nil tag:0];
UITabBarItem *albumItems=[[UITabBarItem alloc] initWithTitle:@"Album" image:nil tag:1]; //the tag is how you tell what was clicked
[items addObject:homeItem];
[items addObject:albumItems];
//MyTabBar is of type UITabBar
myTabBar=[[UITabBar alloc] initWithFrame:CGRectMake(0,411,320,49)];
[myTabBar setItems:items];
myTabBar.delegate=self; //you gotta implement the UITabBar delegate protocol
[myTabBar setSelectedItem:eventItem]; //set the selected item
[homeItem release];
[eventsItem release];
[albumItems release];
[items release];
[self.view addSubview:myTabBar]
}
那麼協議方法看起來像下面那樣 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)ite米 { 如果(item.tag == 0){ // 加載,涉及到該項目的視圖控制器,並釋放其他 } ...等
}
不知道爲什麼你會想要做到這一點,如果涉及內存問題,當前選項卡將無論如何都會被卸載。這就是--viewWillAppear,-viewDidUnload等等。
我可以回答在一兩個問題...
你只需要充當的UITabBarController委託一類,然後實現的方法,像這樣:
// Animate tab selections so they fade in and fade out
-(void)tabBarController:(UITabBarController*)tbc didSelectViewController:(UIViewController*)newSelection
{
[UIView beginAnimations:@"TabFadeIn" context:nil];
[UIView setAnimationDuration:0.6];
for(UIViewController* vc in tbc.viewControllers)
vc.view.alpha = (vc==newSelection) ? 1 : 0;
[UIView commitAnimations];
}
現在我的代碼只是使標籤欄淡入淡出,但您也可以在這裏工作來卸載未使用的標籤頁。如果某些選項卡使用大量內存,有時候這是個好主意。
謝謝我可能會使用如果我決定讓過渡動畫。 – mk12 2009-08-14 04:18:43
UITabBarController延遲加載其所有視圖控制器。當一個標籤被切換出來時,它的視圖可能會在內存緊張的情況下被釋放。當它被第二次選擇時,它被重新創建。此外,大部分內存命中都在視圖中,而不是視圖控制器。因此,不要擔心視圖控制器的內存命中。這個觀點就是這個過程。
如果您正在操作系統的v3上運行,那麼您可以使用-viewDidUnload方法來確保減少內存的最大量。
Andrew
延遲加載不是UITabBarController任務。相反,它是與您的Tab關聯的viewControllers的可責任性。
要釋放與每個UIViewControllers關聯的UIView,每次更改TabBarItem時,必須在與您的UITabBarController關聯的每個UIViewController子類中實現以下方法。viewControllers屬性:
-(void)viewDidDisappear {
[self.view removeFromSuperview];
self.view = nil;
}
顯然,這將刪除與您的UIViewController關聯的self.view。但是,如果你的代碼足夠聰明,這將刪除所有相關的對象。 例如,假設你的loadView方法如下:
-(void)loadView {
UIView *contentVew = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.view = contentView;
…
...
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,50)];
…
…
[contentView addSubview:aLabel];
[aLabel release];
…
[contentView release];
}
這意味着內容查看他們的記憶responsabilities內的每個對象都要求在內容查看,被釋放並連接到self.view屬性。
在這種情況下,除去self.view(即對contentView的引用),導致每個對象的多米諾骨牌式釋放,那就是您的目標。
問候
我目前使用這個卸載標籤欄不活動的視圖控制器(根據Kendall的答案)
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController: (UIViewController *)viewController {
// reload all inactive view controllers in the tab bar
for (UIViewController *vc in tabBarController.viewControllers) {
if(vc != viewController)
[vc didReceiveMemoryWarning];
}}
這是一種糟糕的編碼風格,它使用當前實現方法的副作用來實現目標。 didReceiveMemoryWarning的實現可能會改變並破壞您對其行爲的假設。由於行爲不明確,這也很難調試。 – ikuramedia 2012-01-31 07:26:32
我沒有說我知道如何重新創建uitabbarcontroller,這是我必須要做的事來管理我自己的標籤欄。也許我可以嘗試.. – mk12 2009-08-13 19:23:13
它不難,你只需要實現一個方法做了selectItem,然後你做你的設置取決於選擇的項目,非常直接,你可以做這種方式的延遲加載 – Daniel 2009-08-13 19:35:10
那麼你說我應該讓我自己的uiviewcontroller類懶加載和使用標籤欄或使用UITabBarController?我嘗試了didSelectItem方法,我無法讓它正常工作。 – mk12 2009-08-13 23:42:02