2010-05-31 53 views
2

我有UITabBar在視圖中有5個選項卡。我正在使用didSelectItem委託來打開不同的視圖,即我不使用TabBarController如何設置在iPhone上編程選擇第一個TabBar

我的問題是在視圖加載我需要第一個選項卡默認情況下被選中。 TabBar中是否有任何屬性可以讓它選中?

謝謝。

+0

試試這個:http://stackoverflow.com/a/11201924/1463604 – Nishant 2015-09-18 09:11:36

回答

14

此代碼將工作[tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];

+0

+1表現很好 – Warewolf 2013-04-26 06:48:37

0

設置 tabbar.selectedItem = 0;在viewWillAppear中,所以當視圖出現時,它會默認選擇第一個選項卡。

+0

它不工作...... [ tabBar setSelectedItem:<#(UITabBarItem *)#> 我已經使用IB來創建TabBar。 – iPhoneDev 2010-06-01 04:43:48

0
[self.tabBar setSelectedItem:self.tabBar.items[0]]; 

self.tabBar.selectedItem = self.tabBar.items[0]; 

SelectedItem屬性需要TabBarItem而不是指數。因此,在第一個選項卡的索引0中提供tabbaritem。

這是錯誤的然後:tabbar.selectedItem = 0;

您也可以選擇其他標籤。快樂編碼

4

在SWIFT如果的TabBar使用不tabbarcontroller組默認選擇

var tabbar:CustomTabBar?//if declare like this 

    tabbar!.selectedItem = self.tabbar!.items![0] as? UITabBarItem 

or 

    let tabbar = UITabBar()//if declare and initilize like this 

    tabbar.selectedItem = self.tabbar.items![0] as? UITabBarItem 
+0

grt像魅力一樣工作...... – 2015-06-17 11:30:44

+0

它會崩潰並顯示直接修改標籤欄管理的標籤欄控制器是不允許的。 – 2018-01-25 06:02:15

相關問題