Setting the title of a navigation bar inside a tab bar controller
Solution 1
Basically I don't know if you did't noticed but I got it work like this:
I assigned my tab bar Controller a new class.
I did the following:
add it to app delegate.h
@property (nonatomic,retain) NSString *center;
add it to mainmap.m
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.center=[centers objectAtIndex:indexPath.row];
and in mytabbarcontroller.m
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
ski_center=delegate.center;
self.navigationItem.title = ski_center;
Solution 2
Simple!
[self.tabBarController setTitle:@"Title"];
Solution 3
Adding on to ghostrider's solution, in the mytabbarcontroller.m self.navigationItem.title didn't work for me. I have a NavigationController whose detail view is a TabController and this is what I have in my first tab implementation
self.navigationController.visibleViewController.title = ski_center;
Solution 4
In Swift 4 and above
self.tabBarController?.title = "Title"
Solution 5
Ok just so you know, it's very bad practice to put a UITabBarController
inside a UINavigationController
- Apple recommends for good UI design that you only should ever put a UINavigationController
inside a UITabBarController
and not the other way round. Either way, you should be able to change the title from the UITabBarController
like this;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[self.navigationController.viewControllers objectAtIndex:0] setTitle:ski_center];
}
That should work.
ghostrider
Updated on July 11, 2020Comments
-
ghostrider almost 4 years
Well in my app I have a navigation controller with a table view, and when pressing a button a tab controller opens. In its first tab, I want the title of the navigation bar to be set.
Here is an image of my storyboard just to make sure we undestand each other.
This is my code that supposed to be working. I had used this code, when trying to open a single view and not a tab controller and it was working properly. So I guess I must change something.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { SkiCenter *myDetViewCont = [[SkiCenter alloc] initWithNibName:@"SkiCenter" bundle:[NSBundle mainBundle]]; myDetViewCont.ski_center = [centers objectAtIndex:indexPath.row]; UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; [[self navigationController] pushViewController:[storyBoard instantiateViewControllerWithIdentifier:@"SkiCenterIds"] animated:YES]; //[self.navigationController pushViewController:myDetViewCont animated:YES]; // "Pushing the controller on the screen" [myDetViewCont release]; // releasing controller from the memory myDetViewCont = nil; }
Where
Skicenter
is the name of the class for my first tab andSkiCenterIds
is the identifier of my tab controller in storyboard.Code in
Skicenter
.m is:- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.navigationItem.title = ski_center; }
but I do not see any title. So what I have done wrong? I have also tried this:
[self.navigationController setTitle:@"Live"];
or
self.title=ski_center;
the ski_center has value because it is printed out normally in the NSLog.
-
Wasim over 11 yearsI've updated my answer, please have a look and see if that works.
-
ghostrider over 11 yearsIt didn't work either. what do you suggest me to do? I tried this structure because from a table I want to open a tab menu and not the first screen that opens to be a tab.
-
Wasim over 11 yearsIf it was me, I would forget the tab view and just open up another table view with 1-5 different options that you would usually have on your tabs, then link each row to your new pages. Alternatively you can open up another viewcontroller and create some buttons on it, again linking each button to your new pages. Both these solutions are better than the tab view controller for what you need it for. That way, the navigation controller can easily show titles and the process will make more sense to the end user.
-
Ingweland almost 8 yearsThis was the only solution that worked for me. My set up - UINavigationControllers inside UITabbarController (which is root view controller). Somehow, all other obvious solution did not work at all.
-
Suraj K Thomas over 7 yearsThe title text disappears on switching between the viewcontrollers
-
Mike Critchley over 6 yearsThis should be the correct answer. Nicely done. Thanks for the help.
-
ralphgabb almost 3 yearsThis is a good advice and also make sense, thank you for pointing me to a right direction. Cheers
-
CyberMew over 2 yearsCan I get the reference link where Apple says that it is not good UI design to do this?