2011-09-09 104 views
7

對於我的應用程序中的導航,我正在使用UITabBarController。這工作正常,但在我的一個視圖控制器中,我想將另一個視圖控制器放到tabbar視圖中。 換句話說,我想用另一個替換選定的視圖控制器。 我用下面的代碼這樣做:將另一個視圖控制器推入UITabBarController視圖

self.tabBarController.selectedViewController = self.otherViewController; 

viewControllers在我的TabBarController不包含otherViewController列表。 這個技巧在IOS 4.3中運行正常,但IOS 5不喜歡它。

有沒有人知道IOS 5接受的解決方案?

回答

12

要替換該視圖控制器與其他視圖控制器使用TabBar? 如果是這樣,你必須通過設置一個新的tabbar來編輯tabbar中的viewControllers屬性。這將是這樣的:

UIViewController *thisIsTheViewControllerIWantToSetNow; 
int indexForViewControllerYouWantToReplace; 

NSMutableArray *tabbarViewControllers = [self.tabbar.viewControllers mutableCopy]; 

[tabbarViewControllers replaceObjectAtIndex:indexForViewControllerYouWantToReplace withObject:thisIsTheViewControllerIWantToSetNow]; 

self.tabbar.viewControllers = tabbarViewControllers; 

[tabbarViewControllers release]; 
+0

這工作,但你必須採取的NSMutableArray(你的viewController陣列的mutableCopy) –

+0

哦,是的,你說得對,我打字,沒有Xcode中,使我意識到replaceObjectAtIndex: withObject不存在,除非它是一個NSMUTABLEArray –

+0

這是wỏk,但tabbar的圖標丟失,當我觸摸另一個選項卡並返回,當改變視圖時它是「滯後」,我認爲 – user1561904

0

你不能在這個標籤上使用導航控制器或類似的東西嗎?

反正這應該工作:

NSMutableArray *controllers = [NSMutableArray arrayWithArray:rootTabBarController.viewControllers]; 
[controllers replaceObjectAtIndex:rootTabBarController.selectedIndex withObject: newController]; 
rootTabBarController.viewControllers = controllers; 
相關問題