2012-03-27 152 views
3

我是iOS開發新手,我已經直接從IOS 5開始。我創建了一個由tabview控制器作爲其rootview控制器的故事板。我已經把2個標籤。取消選擇或取消選擇iOS 5中tabbar中的所有選項卡

我想先取消/取消選擇所有選項卡。我該怎麼做呢?我已經嘗試了以下內容

UIView *view = [[UIView alloc]initWithNibName:@"view" bundle:[NSBundle mainBundle]]; 
    [self.tabBarController setSelectedViewController:nil]; 
    [self.tabBarController setSelectedViewController:view]; 

其中我添加了一個標識符爲「view」的視圖。

但這並沒有工作,它給了錯誤:

unrecognized selector sent to instance 

我也試過以下

[self.tabBarController.tabBar setSelectedItem:nil]; 

但它說

'NSInternalInconsistencyException', reason: 'Directly modifying a tab bar managed by a tab bar controller is not allowed.'

我曾嘗試在此代碼第一個選項卡的控制器。我想這樣做是因爲我想在第一個選項卡視圖頂部放置一個默認視圖,並在下面的任何選項卡上單擊使用後將其隱藏。

回答

7

我用它來清除任何選擇tabBarItems

[myTabBar setSelectedItem:nil]; 
+0

任何方式設置動畫? – 2014-11-10 12:59:42

4

老問題。僅用於記錄,如果選項卡欄由TabBarController管理,則無法取消選擇所有選項卡。方法:

[self.tabBar setSelectedItem:nil];

作品只有如果標籤欄從標籤欄控制器來管理。如果是,則無法取消選擇所有選項卡,必須始終選擇一個選項卡。

0

我發現了一個解決方案,可能不是最好的方法去解決它,但它對我有用([self.tabBar setSelectedItem:nil];沒有)。我把我的標籤視圖的父視圖(myTabBarViewContainer),然後當我想取消我的項目我打電話removeFromSuperview父視圖並重新初始化它:

[self.myTabBarViewContainer.view removeFromSuperview]; 

然後重新初始化它,瞧!

不是很漂亮,但它的作品...

1

這是一個小hoakey。但是當我想讓它看起來沒有選擇時,我只需將-viewWillAppear中的色調設置爲灰色,然後在我想顯示選擇時將色調顏色設置回「選定」的顏色。

// since we cant turn off tabbar selection, change the color to make 
// it look like nothing was selected. 
self.tabBarController.tabBar.tintColor = [UIColor grayColor]; 
----------- 
// make sure tintcolor is turned on (just in case another module turned it off 
self.tabBarController.tabBar.tintColor = self.view.tintColor; 
-1

您應該需要使用UITabBar。不是的UITabBarController

取消或取消的TabBar項目SWIFT 3.0

tabbar_Main.selectedItem = nil 
相關問題