2010-07-10 66 views

回答

12

由於標籤欄控制器是一個容器視圖控制器,用於將應用程序劃分爲兩種或多種不同的操作模式,因此大多數應用程序都將導航控制器作爲子項的標籤欄控制器。

蘋果的地位是這樣的:

您在 情況下使用標籤欄控制器,你的程序 要麼呈現出不同類型的 數據或呈現 顯著不同的方式相同的數據。

這並不是說你不能做不同的事情......你主要的問題是,你已經放置在導航控制器的應用程序,你想通過程序生成標籤欄控制器。因此,我可以看到這一點的唯一方法是,您不介意每次更換導航控制器內的屏幕時,Tabbar控制器是否更改。有些應用程序可以這樣工作大多數不。

如果我上面的假設是真實的,我建議你重新考慮你的代碼,看看你是否想要追求這一發展路線。如果是這樣,您可以輕鬆創建一個TabBar控制器並將其附加到當前視圖中。

下面是代碼,我用它來創建我的設置爲我的應用程序之一:

// set up a local nav controller which we will reuse for each view controller 
UINavigationController *localNavigationController; 

// create tab bar controller and array to hold the view controllers 
UITabBarController *tabBarController = [[UITabBarController alloc] init]; 

NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:1]; 

// setup the first view controller (Root view controller) 
RootViewController *myViewController; 
myViewController = [[RootViewController alloc] initWithTabBar]; 

// create the nav controller and add the root view controller as its first view 
localNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController]; 
localNavigationController.navigationBar.barStyle = UIBarStyleBlack; 
localNavigationController.delegate = self; 

[localControllersArray addObject:localNavigationController]; 

// release since we are done with this for now 
[localNavigationController release]; 
[myViewController release]; 

tabBarController.viewControllers = localControllersArray; 
tabBarController.moreNavigationController.navigationBar.barStyle = UIBarStyleBlack; 

tabBarController.delegate = self; 
tabBarController.moreNavigationController.delegate = self; 

// release the array because the tab bar controller now has it 
[localControllersArray release]; 

self.tabBarController.selectedIndex = 0; 

// add the tabBarController as a subview in the window 
[window addSubview:tabBarController.view]; 

// need this last line to display the window (and tab bar controller) 
[window makeKeyAndVisible]; 

有,我覺得這是比較容易編程做一切,所以很多情況下。

希望這會有所幫助。

0

您必須保留對標籤欄控制器的引用。例如,您可以將它保留在App Delegate中...