2010-03-22 56 views
2

我是儘量避免使用Interface Builder如何在沒有XIB的情況下將選項卡欄添加到現有的視圖控制器

目前我有通過代碼創建視圖控制器,並通過代碼更改視圖。

我現在需要的步驟之一發送到應用程序一個新視圖與標籤欄,這將讓我改變看法也是如此。

理想情況下,我要做的是告訴當前的視圖控制器添加一個標籤欄的底部,但我不知道如果這是可行的,所以我可能不得不與UITabBarController交換UIViewController?

任何幫助將不勝感激。

乾杯, 安德烈

回答

5

我沒有手頭上的XCode,所以我會盡量口頭回答。

創建一個新的UITabBarController並將當前視圖設置爲root view,然後添加儘可能多的選項卡(每個選項卡都有自己的視圖)。

UPDATE
init'ing控制器後,限定視圖的陣列(加入是很重要的順序)。並調用這個標籤欄控制器

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated 

更新2

這裏的是一個簡單的代碼來創建兩個空的觀點標籤欄,每個都有自己的標籤按鈕。

tabBarController = [[UITabBarController alloc]init]; 

firstView = [[FirstView alloc] init]; 
UITabBarItem *item1 = [[[UITabBarItem alloc]initWithTitle:@"First" image:nil tag:1] autorelease]; 
[firstView setTabBarItem:item1]; 

secondView = [[SecondView alloc] init]; 
UITabBarItem *item2 = [[[UITabBarItem alloc]initWithTitle:@"Sec" image:nil tag:1] autorelease]; 
[secondView setTabBarItem:item2]; 

[tabBarController setViewControllers:[NSArray arrayWithObjects:firstView,secondView,nil] animated:NO]; 

[window addSubview:tabBarController.view]; 

當然這個代碼不會是因爲是有用的,你需要手動創建視圖,或者爲每個視圖創建一個筆尖文件,並在initWithNibName

更新3
檢查加載這Stanford iPhone Course,它從斯坦福大學的免費課程。講師是蘋果的員工。第7講標題爲導航&標籤欄控制器將給你在這些組件上的良好開端。

+0

謝謝:)我已將它添加到視圖中,它出現了,空白的畫布和底部的空欄。 現在我想弄清楚如何告訴標籤欄在白色畫布上顯示特定視圖以及如何將4個按鈕添加到底部的空白標籤欄中。 – Andre 2010-03-22 14:01:26

+0

@Andres,更新答案 – medopal 2010-03-22 14:08:26

+0

我已將我的具體視圖添加到白色畫布。我正在使用:tabBarController = [[UITabBarController alloc] init]; \t \t [tabBarController setViewControllers:[NSArray arrayWithObjects:viewController,nil]]; viewController.view = step3;其中「step3」是我想要展示的視圖。現在我只是試圖添加實際的標籤按鈕和關聯動作來改變視圖 – Andre 2010-03-22 14:14:19

相關問題