如何从子视图的View Controller设置导航栏的标题?

Shu*_*rma 3 iphone iphone-sdk-3.0 ios4 ios

我有一个视图控制器,在启动时有导航栏.
在一些UIViewConrollers之后,我添加了一个tabbar控制器,我为每个标签栏创建了四个标签栏和四个视图控制器.
现在如果我创建带有与每个选项卡相关的导航栏的标签栏,它会在顶部显示两个导航栏,如果创建没有导航栏的标签栏,它将只显示一个标签栏,但我无法将与每个标签视图控制器对应的标题添加到该导航bar
这是我创建标签栏的代码

EventDetailViewController *firstViewController = [[EventDetailViewController alloc]init];
firstViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Home" image:[UIImage imageNamed:@"Multimedia-Icon-Off.png"] tag:2]autorelease];                                 MoreInfo *secondViewController = [[MoreInfo alloc]init];
secondViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Info" image:[UIImage imageNamed:@"Review-Icon-Off.png"] tag:0]autorelease];

PlaceInfoViewController *thirdViewController = [[PlaceInfoViewController alloc]init];
thirdViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Lugar" image:[UIImage imageNamed:@"Place-icon-OFF.png"] tag:1]autorelease];

FavoritesViewController *forthViewController = [[FavoritesViewController alloc]init];
forthViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Compartir" image:[UIImage imageNamed:@"Compartir-Icon-Off.png"] tag:3]autorelease];

UITabBarController *tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController.viewControllers = [[NSArray alloc] initWithObjects:firstViewController, secondViewController, thirdViewController, forthViewController, nil];
tabBarController.view.frame = self.view.frame;
[self.view addSubview:tabBarController.view];

[firstViewController release];
[secondViewController release];
[thirdViewController release];
[forthViewController release];
Run Code Online (Sandbox Code Playgroud)

Ans*_*ala 6

您可以获取对视图控制器的引用navigationItem并设置它title.

[[aViewController navigationItem] setTitle:@"My Title"];
Run Code Online (Sandbox Code Playgroud)

请注意,如果您还没有将所有视图控制器嵌入到导航控制器中,则需要将它们嵌入到导航控制器中.这样他们实际上会得到带导航项的导航栏,你不必自己绘制任何一个.

NSMutableArray *tabs = [NSMutableArray array];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[tabs addObject:nav];
nav = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[tabs addObject:nav];
...

[tabBarController setViewControllers:tabs];
Run Code Online (Sandbox Code Playgroud)

编辑:您的标签栏控制器位于导航控制器内,因此您需要首先获得对标签栏控制器的引用.尝试[[[self tabBarController] navigationItem] setTitle:...]使用viewDidLoad视图控制器的方法.