2012-09-25 48 views
4

我已閱讀關於在應用程序委託和界面構建器中添加tabbar圖標的不同答案。我想將它們添加到我的firtsviewcontroller中,因爲我想從plist中讀取圖標圖像名稱。 建議做這樣的:Xcode動態添加tabbar圖標

NSArray *viewControllerArray = nil; 
viewController1 = <View Init Code> 
viewController2 = <View Init Code> 
viewController3 = <View Init Code> 

1stNavController = [[UINavigationController alloc] initWithRootViewController:viewController1]; 
UIImage *img = [UIImage imageNamed:@"tab_home"]; 
[1stNavController .tabBarItem initWithTitle:@"Home" image:img tag:1]; 

但我不喜歡做這種方式,因爲我覺得不乾淨。我想做類似以下的事情,但我不知道爲什麼它不工作:

[[self.tabBarController.tabBar.items objectAtIndex:2] setIcon:[UIImage imageNamed:....]]; 

任何解決方案?

+2

運行到你形容爲「清潔的解決方案」死端之前,考慮到用戶可能會重新排列的TabBar項(通過更多,編輯)的方法可行。這可能不適用於您的應用程序,但它肯定是SDK開發人員所考慮的一個普遍問題。 – Till

+0

我的應用程序將有5個tabbaritems,它的設計是固定的,用戶將無法選擇重新排列圖標。在我的應用程序中重新排列tabbaritem圖標是沒有意義的。我想從我的第一個控制器中的Plist中讀取de tabbaricon圖像 – Aitul

回答

0

如何像:

NSArray *viewControllers = self.tabBarController.viewControllers; 
((UIViewController*)viewControllers[0]).tabBarItem.image = [UIImage imageNamed:...]; 
((UIViewController*)viewControllers[1]).tabBarItem.image = [UIImage imageNamed:...]; 
((UIViewController*)viewControllers[2]).tabBarItem.image = [UIImage imageNamed:...]; 
((UIViewController*)viewControllers[3]).tabBarItem.image = [UIImage imageNamed:...]; 
((UIViewController*)viewControllers[4]).tabBarItem.image = [UIImage imageNamed:...];