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

时间:2012-07-17 05:34:54

标签: iphone ios ios4 iphone-sdk-3.0

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

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];

5 个答案:

答案 0 :(得分:6)

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

[[aViewController navigationItem] setTitle:@"My Title"];

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

NSMutableArray *tabs = [NSMutableArray array];

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

[tabBarController setViewControllers:tabs];

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

答案 1 :(得分:2)

请转到EventDetailViewController.m并定义以下方法。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
    self.title = NSLocalizedString(@"Catalogues", @"Catalogues");
    //self.tabBarItem.image = [UIImage imageNamed:@"book"];

}
return self;
}

希望它能在每个viewController.m中工作,定义上面的初始值。

答案 2 :(得分:0)

您无法为tabbar控件设置标题。您必须在每个viewcontrollers上使用UINavigationBar

对于UINavigationBar:  将导航栏拖动到您的xib / nib然后添加标题或以编程方式执行

答案 3 :(得分:0)

我自己通过将父级导航项的引用传递给子视图控制器来获得解决方案。大家帮忙......:)

答案 4 :(得分:0)

您无法设置导航栏的标题。在导航中设置视图或子视图的标题。 self.title = @ “欢迎”;只需设置标题。