2011-01-10 69 views
7

我已經設置了一個帶有兩個選項卡的UITabBarController,一個是簡單的UIViewController,另一個是使用第二個UIViewController作爲它的rootController(稍後將用於設置UITableView)的UINavigationController。我的問題是關於命名選項卡(即設置每個UITabBarItem的標題)從UINavigationController設置UITabBarItem標題?

對於第一個選項卡(簡單的UIViewController),我已將下列(見下文)添加到controllers -init方法中。

// FROM -[MapController init] 
- (id)init { 
    self = [super init]; 
    if(self) { 
     UITabBarItem *tabBarItem = [self tabBarItem]; 
     [tabBarItem setTitle:@"ONE"]; 
    } 
    return self; 
} 

對於另一個選項卡,我已經添加(見下文)到第二個控制器init(rootController)。

// FROM -[TableController init] 
- (id)init { 
    self = [super init]; 
    if(self) { 
     UITabBarItem *tabBarItem = [[self navigationController] tabBarItem]; 
     [tabBarItem setTitle:@"TWO"]; 
    } 
    return self; 
} 

難道我設置第二tabBarItem標題在正確的地方作爲目前它沒有顯示,當我運行的第一個標籤寫着「ONE」,二是空的應用程序。另外tabBarItem(上面)顯示nil,當我打印值或通過調試器查找。

EDIT_001:從AppDelegate中

添加完整的代碼,我可以正確地從AppDelegate中內設置的UITabBarItem當我第一次創建控制器,準備增加了的UITabBarController。但我真的想在個人控制器中做到這一點 - 整潔的初始方法。

// UITabBarController 
UITabBarController *tempRoot = [[UITabBarController alloc] init]; 
[self setRootController:tempRoot]; 
[tempRoot release]; 
NSMutableArray *tabBarControllers = [[NSMutableArray alloc] init]; 

// UIViewController ONE 
MapController *mapController = [[MapController alloc] init]; 
[tabBarControllers addObject:mapController]; 
[mapController release]; 

// UITableView TWO 
TableController *rootTableController = [[TableController alloc] init]; 
UINavigationController *tempNavController = [[UINavigationController alloc] initWithRootViewController:rootTableController]; 
[rootTableController release]; 
[tabBarControllers addObject:tempNavController]; 
[tempNavController release]; 

[rootController setViewControllers:tabBarControllers]; 
[tabBarControllers release]; 

[window addSubview:[rootController view]]; 
[window makeKeyAndVisible]; 

EDIT_002:溶液到目前爲止,更新UITabBarItems在應用程序委託例如

// TABLE CONTROLLER 
TableController *tableController = [[TableController alloc] init]; 
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tableController]; 
[tableController release]; 
[tabBarControllers addObject:navController]; 
UITabBarItem *tabBarItem_002 = [navController tabBarItem]; 
[tabBarItem_002 setTitle:@"TWO"]; 
[tabBarItem_002 setImage:[UIImage imageNamed:@"GT_TWO.png"]]; 
[navController release]; 

EDIT_003: 有設置UITabBarItem在UINavigationController的方式或者是委託(因爲它似乎)真正做到這一點的最佳場所。在頭文件

@interface yournavController : UIViewController<UITabBarDelegate> 

,並在init方法

self.navigationController.tabBar.delegate=self; 
    [self.navigationController.tabBar setTitle:@"TWO"]; 

希望這將有助於

回答

10

你實際上必須自己創建tabBarItem,然後將其分配給視圖控制器的tabBarItem屬性。嘗試在你的初始化:

UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"TWO" 
                 image:[UIImage imageNamed:@"GT_TWO.png"] 
                  tag:0]; 
self.tabBarItem = tabBarItem; 
[tabBarItem release]; 

希望這會有所幫助。

+1

self.navigationController.tabBarItem = tabBarItem是什麼爲我工作,如果有人想嘗試不同的解決方案! – Rabiees 2013-04-21 20:47:03

0

試試這個不是導航控制器的一部分。這就是爲什麼tabbaritem變量爲零。

不知道這是否可行,但您可以配置tableviewcontroller的tabbaritem。有一點運氣,封閉的navigationcontroller會繼承它。 (多數民衆贊成它如何與標題屬性)

3

雖然init'ing的tableviewcontroller

,它 -

+0

嗨克里斯啊,這是一個很好的觀點,我沒有發現這一點。 – fuzzygoat 2011-01-10 23:34:32

2

我採取了在AppDelegate中爲我一直在努力的項目創建輔助方法的方法。它幫助我集中設置標籤欄控制器上視圖控制器的圖標和標題的邏輯,並且只需編寫一次即可。

- (void)setIconAndTitleForViewController:(UIViewController *)viewController iconName:(NSString *)iconName titleKey:(NSString *)titleKey 
{ 
    NSString *iconPath = [[NSBundle mainBundle] pathForResource:iconName ofType:@"png"]; 
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:iconPath]; 
    viewController.tabBarItem.image = image; 
    [image release]; 
    viewController.title = NSLocalizedString(titleKey, @""); 
} 

你會然後調用它類似於:

UIViewController *viewController = [[UIViewController alloc] initWithNibName:@"SomeNibName" bundle:[NSBundle mainBundle]]; 
[self setIconAndTitleForViewController:viewController iconName:@"AnIcon" titleKey:@"SomeKey"]; 

然後在視圖控制器添加到該的UITabBarController UIViewControllers的陣列。

0

就像馬可說的,你必須立即執行這個項目,根據你的意願配置它,然後分配它。其實它是在蘋果文檔:)

0

我有完全相同的問題,並發現你的問題接受的答案相當有幫助。然而,我來到以下解決方案:

  1. 在故事板(或IB管理標籤欄)內我已設置標籤欄項目的標題不是它們的標題,而是所有本地化中使用的鍵字符串「Localizable.strings」文件。在我的情況下,這是相應的德語文本。在許多其他情況下,這將是英文版本,甚至是任何形式的唯一關鍵字。
  2. Witin我的應用delegat在didFinishLaunchingWithOptions我加入:

for (UIViewController *vc in self.window.rootViewController.childViewController) { vc.tabBarItem.title = NSLocalizedString(vc.tabBarItem.title, @"Tab Bar Title"); }

在這個時間點,標籤欄完全加載,所有(最上面的)視圖控制器完全初始化而僅視圖控制器第一個標籤的內容已完全加載。 viewDidLoad中的任何內容,正如您嘗試的,以及我首先嚐試的那樣,並不是很有幫助。在init方法中設置標題並沒有幫助,因爲這些值被故事板覆蓋。 現在TabBarItems的標題可能看起來不太專業。但是在這個時候將它們轉換爲本地化的等價物是個訣竅。

相關問題