Question

I have a TabBarController that contains 5 TabBarItems, i want to have a control over those items so i create a new class that extends UITabBarController and make it the delegate for the TabBarController <UITabBarControllerDelegate> then i bind the TabBarController on the story board with this class, but I'm unable to create any outlet for any UITabBarItem also i tried to print the titles of each TabBarItem in the viewWillAppear method as follows:

-(void)viewWillAppear:(BOOL)animated{
for (int i=0; i<5; i++) {
    UITabBarItem *tabItem=[self.tabBarController.tabBar.items objectAtIndex:i];
    NSLog(@"Title:-------->%@",[tabItem title]);
}

}

but it prints Null foreach item title, what can i do to have a control over those TabBarItems ?

Was it helpful?

Solution

The problem is the way you are accessing the UITabBar.

As you said, you already have this code placed in the class that is inherited from UITabBarController, then you should access the UITabBar as

self.tabBar

instead of

self.tabBarController.tabBar

Try using this efficient code:

for(UITabBarItem *item in [self.tabBar items]) {
    NSLog(@"Title: %@", [item title]);
}

Here is the attached sample project.Project attached

OTHER TIPS

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        self.title = NSLocalizedString(@"Favorites", @"Favorites");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
    }
    return self;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top