Eslam Hamdy
Eslam Hamdy

Reputation: 7386

Unable to access UITabBarItem(s) inside UITabBar

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 ?

Upvotes: 0

Views: 299

Answers (2)

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

- (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;
}

Upvotes: 0

Balram Tiwari
Balram Tiwari

Reputation: 5667

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

Upvotes: 1

Related Questions