self.title sets navigationController and tabBarItem's title? Why?
Asked Answered
S

4

58

I do this in a UIViewController for one of my tabs:

self.title = @"Welcome";

However, it's overwriting whatever I have for the tabBarItem. I have tried:

self.tabBarItem.title = @"Home";

and

[self.tabBarItem initWithTitle:@"Home" image:[UIImage imageNamed:@"iconHome.png"] tag:0];

But still, self.title overwrites the tabBarItem, regardless of whether I am trying the two latter pieces of code after the title has been set. The code even runs without errors, but the self.tabBarItem.title or initWithTitle doesn't do anything?

Sefton answered 8/10, 2009 at 21:53 Comment(0)
S
171

OK, I figured it out! Here's what I am doing:

self.title = @"Title for TabBarItem"; // TabBarItem.title inherits the viewController's self.title
self.navigationItem.title = @"Title for NavigationBar";

the navigationBar would inherit self.title, unless otherwise set using self.navigationItem.title

Sefton answered 20/10, 2009 at 17:54 Comment(3)
I was running into exactly the same problem when using a UINavigationController within a UITabController. This solved it.Antre
This works for me. Great for changing just the top title based on content even if the view controller doesn't change.Quirites
Quick note that the order of these two lines matters.Bumpy
B
60
//set nav item title
self.navigationController.navigationBar.topItem.title = @"zurück";

this did it for me :=) (nothing of the above worked)

Banter answered 14/3, 2011 at 12:28 Comment(0)
H
0

Try:

[self setTitle:@"Welcome"];

UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Home" image:[UIImage imageNamed: image] tag:0];
[self setTabBarItem:item];
[item release];
Hubbs answered 8/10, 2009 at 22:17 Comment(3)
Hmm. Doesn't work either. "Welcome" (title for self) is always the same for the tabBar. Very frustrating, actually. I spoke to a dev friend who said the only way he was able to do it differently, was to set the title of the navigationController in IB, and then not set self.title in the code. But that just seems odd. If no one comes with a solution here, I might file a bug report to Apple.Sefton
Out Of interest where are you doing your tab bar setup? The sample above comes from the init method of my controller. I don't use interface builder for my views (their either custom or created in loadView). That may be the difference.Hubbs
Yeah, I am using IB, but tried to init the tabs in each UIViewController (NavigationController). I'll have a look at what happens if I place the above code in an init method in the UIViewController...Sefton
T
0

I was also facing the same issue, but i solve this issue like this. I set the title and image of tabBarItem right after i created them in appDelegate.

This is what i have done:

[viewController setTitle:@"controllerTitle"];
[[viewController tabBarItem] setTitle:@"Custome Title for tab"];
[[viewController tabBarItem] setImage:[UIImage imageNamed:@"tab.png"]];
Twophase answered 30/8, 2010 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.