1

我目前正在學習iOS開發,而且我很困惑內部如何設置標題。在TabBarController中使用NavigationController時的標題

我在爲iPad基於窗口的模板下面的代碼,我做的一切從頭開始,而不界面生成器:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
UISplitViewController *svc = [[UISplitViewController alloc] init]; 

UINavigationController *leftNav = [[UINavigationController alloc] init]; 
UIViewController *leftView = [[LeftViewController alloc] init]; 

[leftNav pushViewController:leftView animated:NO]; 
[leftView release]; 

UINavigationController *rightNav = [[UINavigationController alloc] init]; 
UIViewController *rightView = [[RightViewController alloc] init]; 
[rightNav pushViewController:rightView animated:NO]; 
[rightView release]; 

UITabBarController *tbc = [[UITabBarController alloc] init]; 
tbc.viewControllers = [NSArray arrayWithObjects:leftNav, nil]; 
[leftNav release]; 

svc.viewControllers = [NSArray arrayWithObjects:tbc, rightNav, nil]; 
[tbc release]; [rightNav release]; 

self.window.rootViewController = svc; 
[self.window makeKeyAndVisible]; 
return YES; 
} 

我想擁有的TabBarItem自定義簡短標題LeftViewController和頂部的NavigationController欄的長標題。嘗試幾次後,我發現下面是實際工作(代碼爲LeftViewController:viewDidLoad中):

self.title = @"Long title for navigation controller"; 
self.navigationController.title = @"Short title"; 

但是,我不知道爲什麼設置navigationController.title設置標題爲TabBarItem。我試過「self.tabBarItem.title = @」Short title「,而不是它的工作,這更奇怪。

有人可以解釋爲什麼這個工作,以及如何設置標題直接和正確,因爲這真的不右看看我,我覺得我失去了一些重要的事情。謝謝!:)

回答

3

蘋果已經取得了titletabBarItemsNavigationControllers一樣的。就是因爲大多數人使用這種方式併爲開發人員節省了一些時間

你可以做的是創建自己的導航欄作爲該視圖的出口並改變它的標題(你可以在int erface builder,如果你正在使用)或者只是在viewDidLoad方法中設置。

self.yourNavigationBar.topItem.title = @"My New Title"; 
+1

我才發現這是確認一個問題你剛纔說的: http://stackoverflow.com/questions/717844/self-tabbaritem-title-not-working 事實上self.title用於兩個self.navigationItem.title和tabBarItem.title。有趣的是,據其中一位評論員稱,tabBarItem.title無法更改。謝謝! – Svilen

相關問題