views:

265

answers:

2

Right now, I am setting the title in the viewDidLoad of the root view of the tab, which only changes when I click on the tab. I want this to be set before I select the tab. I tried something like:

[[self.parentViewController.tabBarController.tabBar.items objectAtIndex:2] title] = @"string";

in the first view that loads in another tab, but there is clearly something wrong since I get a left operand error.

Can somebody show me the correct way to achieve what I am trying to do?

Thank you!!

A: 
[[self.parentViewController.tabBarController.tabBar.items objectAtIndex:2] title] = @"string";

The syntax is slightly off here. You probably wanted something like:

[[self.parentViewController.tabBarController.tabBar.items objectAtIndex:2].title = @"string";

However, that won't work, since there is no title property to set. In fact, there's no way I can see to change a UITabBarItem's title once it's been initialized. You'll have to use UITabBar's setItems:animated: method to set the entire group of items at once. But it won't be fun.

I bet this would be an Apple HIG violation, which is why there's no easy way to do it with the current API. Rethink your design and ask yourself why you want to change the names of the tabs, which will confuse your users.

Shaggy Frog
Thanks for your response! I'm currently building an app with the Foursquare API, and I noticed that the Foursquare iPhone app has one of the tabs names set to the first name of the user, so I was wondering how they might have done that.
ayanonagon
Likely they do it on initialization of the UITabBar when the application is launching.
Shaggy Frog
+1  A: 

Try setting the title in awakeFromNib instead of viewDidLoad. The view for a view controller is not actually loaded until you need the view, and the tab bar controller by default doesn't access the view of a view controller until you actually select it (which is why you saw the title change when you selected the tab).

Since the nib is creating the view controller to start with (assuming you have built your tab bar controller in IB) awakeFromNib will be called as soon as the view controller has been built, before the tab bar controller can ask what the title is.

Kendall Helmstetter Gelner
Whoa, that worked like magic. I didn't even know that awakeFromNib was even an option for viewController methods. (Learning something new everyday!) Thanks you!!
ayanonagon
awakeFromNib is an option for any object loaded from a nib - the nib loader looks for that method on any object it creates and calls it if found. Note that if you create stuff sometimes from the xib and sometimes from code, you'll have to also override the initWithNibName: method.
Kendall Helmstetter Gelner