在TabBarController中使用NavigationController时的标题

时间:2011-07-22 16:44:51

标签: objective-c cocoa-touch uinavigationcontroller uitabbarcontroller

我目前正在学习iOS开发,我对如何在内部设置标题感到困惑。

我在iPad的基于Window的模板中有以下代码,我在没有Interface Builder的情况下从头开始做任何事情:

- (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;
}

我希望有一个用于LeftViewController的TabBarItem的自定义短标题,以及一个顶部的NavigationController栏的长标题。经过几次尝试后,我注意到以下内容实际上正在运行(代码来自LeftViewController:viewDidLoad):

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

但是,我不知道为什么设置navigationController.title会设置TabBarItem的标题。我尝试了“self.tabBarItem.title = @”短标题“,但它不起作用,这更奇怪。

有人可以解释为什么这样做有效,我怎么能直接和正确地设置标题,因为这对我来说看起来并不合适,我觉得我错过了一些重要的东西。谢谢! :)

1 个答案:

答案 0 :(得分:4)

Apple已为titletabBarItems NavigationControllers提供相同的内容。这是因为大多数人都使用这种方式,这为开发人员节省了一些时间。

您可以做的是创建自己的导航栏作为该视图的插座并更改其标题(如果您使用的话,可以在界面构建器中执行此操作)..或者只需在{ {1}}方法。

viewDidLoad