![](/img/trans.png)
[英]How do I set custom navigation bar(title + titleView) for all navigation controllers and view controller?
[英]How to set title of Navigation Bar from a View Controller of a subview?
我有一个视图控制器,在启动时有导航栏。
在一些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];
您可以获取对视图控制器的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];
编辑:您的标签栏控制器位于导航控制器内,因此您需要首先获得对标签栏控制器的引用。 在视图控制器的viewDidLoad
方法中尝试[[[self tabBarController] navigationItem] setTitle:...]
。
请转到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中工作,定义上面的初始化程序。
您无法为tabbar控件设置标题。您必须在每个viewcontrollers上使用UINavigationBar
对于UINavigationBar:将导航栏拖动到xib / nib,然后添加标题或以编程方式执行
我自己通过将父项的导航项的引用传递给子视图控制器来获得解决方案。大家帮忙... :)
你不能设置导航栏的标题。 在导航中设置视图或子视图的标题。 self.title = @ “欢迎”; 只需设置标题即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.