1

When my app loads I want to change the image on one of the tabs based on a saved setting set by the user the last time they ran the app. I am able to change the image when the user clicks on the tab which executes that tab's viewcontroller's ViewDidLoad method. see below:

UITabBarItem *tabItem;
if (condition = YES) { 
    tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter plus.png"] tag:self.view.tag];
}
else {
    tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter.png"] tag:self.view.tag];
}
self.navigationController.tabBarItem = tabItem;
[tabItem release];
[super viewDidLoad]; 

But I have been unable to figure out how to access and change the UITabBarItem of that tab in the root view controller of the app when it loads. See viewdidload method of root view controller below.

UITabBarItem *tabItem;
if (condition = YES) { 
    tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter plus.png"] tag:self.view.tag];
}
else {
    tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter.png"] tag:self.view.tag];
}
// get the view controller of the tab I want to change
MyViewController *vc = [self.tabBarController.viewControllers objectAtIndex:2];
ft.tabBarItem = tabItem;
[tabItem release];
[super viewDidLoad]; 

When this did not work I tried multiple other ways to access and change the uitabbaritem but nothing worked. I tried creating IBOutLets in the root view controller to UITabBarItem and UINavigationController.

// tb is an iboutlet to the UITabBarItem
self.tb = tabItem;  

// nc is an iboutlet to the UINavigationController
self.nc.tabBarItem = tabItem;   

to no avail. Any idea how to do this?

PeteC
  • 31
  • 1
  • 3

2 Answers2

2

I figured it out. I just needed to call the setImage method of the UITabBarItem.

//UITabBarItem *filterTab = [self.tabBar.items objectAtIndex:2];
if (condition == YES) { 
    [[self.tabBar.items objectAtIndex:2] setImage:[UIImage imageNamed:@"filter plus.png"]];
    // [filterTab setImage:[UIImage imageNamed:@"filter plus.png"]];        
}
else {
    [[self.tabBar.items objectAtIndex:2] setImage:[UIImage imageNamed:@"filter.png"]];
    //[filterTab setImage:[UIImage imageNamed:@"filter.png"]];
}

Follow up question: is there a reason or advantage to assigning the tabbaritem to a pointer first and then setting the image?

UITabBarItem *filterTab = [self.tabBar.items objectAtIndex:2];
[filterTab setImage:[UIImage imageNamed:@"filter plus.png"]];

versus doing it in one line of code as follows?

[[self.tabBar.items objectAtIndex:2] setImage:[UIImage imageNamed:@"filter.png"]];
PeteC
  • 31
  • 1
  • 3
1

I think you need to try this one, hope this will help,

I have change the selected tabbatitem image like -

in tabbar controller delegate method

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

{
    if([tabBarController selectedIndex] == 0)
    {
        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
    }    
}

through this you can change your image.

Or you can use directly in your view controllers init(or ViewWillAppear) method, like

        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];

Try it!!!!

Muhammad Rizwan
  • 3,470
  • 1
  • 27
  • 35