2

I'm working with tabBar Based application with navigation controller. In my application I want to change the tabBarItem image and title. At the time of clicking the pariticular tabBarItem the control goes to viewWillAppear method of that particular view. In this time i want to change image and title of that particular tabBarItem.

I'm giving like this but,it's not working...

Code:

tabItem = [[UITabBarItem alloc] initWithTitle:@"Colors" image:[UIImage imageNamed:@"Colors.png"] tag:9];

self.tabBarController.tabBarItem = tabItem;

Already I'm setting the tag value for that view is 9.

Please help me how can I change the image and title of that particular tabbarItem .

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
kanmani
  • 671
  • 1
  • 10
  • 28

1 Answers1

5

There is a slight misconception in what you are attempting to do. First of all, UITabBarController has no tabBarItem property, which is what you are attempting to set. But, even if it did, the UITabBarController docs specifically state, in regards to UITabBarController's tabBar property:

You should never attempt to manipulate the UITabBar object itself stored in this property. If you attempt to do so, the tab bar view throws an exception.

So, the way to modify the items is not through the UITabBar itself. In your situation, the UITabBar is getting its UITabBarItems from the UIViewControllers represented by each tab. This is something you have control over. Specifically, what you can do is:

- (void)viewWillAppear:(BOOL)animated {
    /* Modifies the UITabBarItem used by a UITabBarController to represent this
     * UIViewController in the tab bar.
     */

    UITabBarItem* tabBarItem =  [[UITabBarItem alloc] initWithTitle:@"Colors" image:[UIImage imageNamed:@"Colors.png"] tag:9];
    self.tabBarItem = tabBarItem;
    [tabBarItem release];
}
Matt Wilding
  • 20,115
  • 3
  • 67
  • 95
  • Hai Matt Wilding, tabItem = [[UITabBarItem alloc] initWithTitle:@"Colors." image:[UIImage imageNamed:@"Colors.png"] tag:9]; self.navigationController.tabBarItem = tabItem; [tabItem release]; The above code is working for all the tabbaritem(having navigationcontroller) except one tabbaritem..how can i solve this issue...please help me to do this...Thank You – kanmani Jan 19 '11 at 07:21
  • You need to provide some context here... What is different about this one? If it represents a view controller of a different class than the other, you'll also need to put that code in it too... – Matt Wilding Jan 19 '11 at 17:50
  • did you mean that, self.tabBarItem.title = @"MyTitile"; will not work? Really its not working in my case. Is it the API rule? But new tab bar item works fine. – karim May 06 '11 at 13:56