2011-05-07 50 views

回答

9

這是很簡單的創建通過UITabBarController中的UITabBar一樣。下面的例子應該在你的AppDelegate類中工作。

應用代表接口

首先,在界面中,我們將定義我們的UITabBarController

UITabBarController *tabBarController; 

應用代理執行

然後,實現文件的application:didFinishLaunchingWithOptions:方法中,我們會再初始化我們的標籤欄控制器。

// Initialise our tab bar controller 
UITabBarController *tabBarController = [[UITabBarController alloc] init]; 

接下來,您需要創建要添加到標籤欄控制器的視圖控制器。我們需要在這些信息中添加一些信息來設置標籤的標題/圖標,但我會在最後回到這一點。

// Create your various view controllers 
UIViewController *testVC = [[TestViewController alloc] init]; 
UIViewController *otherVC = [[OtherViewController alloc] init]; 
UIViewController *configVC = [[ConfigViewController alloc] init]; 

由於setViewControllers:動畫:方法需要視圖控制器的陣列,我們將增加我們的視圖控制器到一個數組,然後釋放他們。 (作爲NSArray的將保留它們。)

// Put them in an array 
NSArray *viewControllers = [NSArray arrayWithObjects:testVC, otherVC, configVC, nil]; 
[testVC release]; 
[otherVC release]; 
[configVC release]; 

然後簡單地提供的UITabBarController與視圖控制器的陣列並將其添加到我們的窗口。

// Attach them to the tab bar controller 
[tabBarController setViewControllers:viewControllers animated:NO]; 

// Put the tabBarController's view on the window. 
[window addSubview:[tabBarController view]];  

最後,確保你的dealloc方法中調用[tabBarController release];

視圖控制器實現

裏面每個視圖控制器,你還需要設置init方法中選項卡的標題和圖標如下:

// Create our tab bar item 
UITabBarItem *tabBarItem = [self tabBarItem]; 
UIImage *tabBarImage = [UIImage imageNamed:@"YOUR_IMAGE_NAME.png"]; 
[tabBarItem setImage:tabBarImage]; 
[tabBarItem setTitle:@"YOUR TITLE"]; 
+0

當我以編程方式創建自己的tabBarControllers時,我總是爲每個選項卡創建一個navigationController,並且每個navigationController都使用rootViewController進行初始化。在我看來,在你的例子中,由於你沒有導航控制器,所以不可能創建一個導航堆棧,但我從來沒有嘗試過這樣編碼。 – 2011-05-07 10:21:57

+0

@沃爾夫岡我想這取決於你是否想要將物品推送/彈出到導航堆棧。 (我的例子是一個相當低端的「只是基礎」的方法。) – 2011-05-07 10:29:52

+0

thnx很多它真的有用 – Kiran 2011-05-07 10:31:00

0

這就是我們要創造的TabBar編程

UINavigationController *BandNavigationController3; 
AudienceSettingsViewController *audienceSettingsViewView =[[AudienceSettingsViewController alloc]initWithNibName:@"AudienceSettingsViewController" bundle:nil]; 
BandNavigationController3 = [[UINavigationController alloc]initWithRootViewController:audienceSettingsViewView]; 
BandNavigationController3.tabBarItem.title = @"Settings"; 
BandNavigationController3.tabBarItem.image = [UIImage imageNamed:@"settings.png"]; 

[BandNavigationController3.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:4]; 
BandNavigationController3.navigationBar.hidden = YES; 

[bandTabBarArray addObject:BandNavigationController3]; 
[BandNavigationController3 release]; 
[audienceSettingsViewView release]; 

[tabBarController setViewControllers:bandTabBarArray]; 
[bandTabBarArray release]; 
+0

-1格式錯誤的複製和粘貼作業,沒有任何解釋。 – 2011-05-07 10:27:36

相關問題