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

10,019

Solution 1

I got the solution myself by passing the reference of the navigation Item of the parent to the child view controller..thanx everyone for helping... :)

Solution 2

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.

Solution 3

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.

Share:
10,019
Shubham Sharma
Author by

Shubham Sharma

Updated on June 05, 2022

Comments

  • Shubham Sharma
    Shubham Sharma almost 2 years

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