简体   繁体   中英

How to set title of Navigation Bar from a View Controller of a subview?

I've a view controller having navigation bar in starting.
After some UIViewConrollers I have added a tabbar controller where I've created four tab bars and four view controllers for each tab bar.
Now if I create tabbars with navigation bars related to each tab it shows two navigation bars at top, if create tabbars without navigation bar it will show only one tabbar but I am not able add title, corresponding to each tab view controller, to that navigation bar
Here is the code in which I created the tab 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];

You can obtain a reference to a view controller's navigationItem and set its title .

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

Note that you'll want to embed all your view controllers in navigation controllers if you've not already done so. That way they'll actually get navigation bars with navigation items and you won't have to draw any of that yourself.

NSMutableArray *tabs = [NSMutableArray array];

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

[tabBarController setViewControllers:tabs];

Edit: Your tab bar controller is inside a navigation controller, so you need to get a reference to the tab bar controller first. Try [[[self tabBarController] navigationItem] setTitle:...] in the viewDidLoad method of your view controllers.

Please go to the EventDetailViewController.m and define the below method.

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

Hope it will work then in each viewController.m define the above initializer.

You cannot set title for tabbar control.You have to use UINavigationBar on every viewcontrollers

For UINavigationBar: Drag Navigation bar to your xib/nib then add the title or do it programmatically

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

you cannot set the title of navigation bar . set the title of view or sub views in navigation. self.title=@"welcome"; simply set the title.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM