Sebastian Roth
Sebastian Roth

Reputation: 11537

UITabBarController.selectedIndex does not change the tabBar icon?

When using UITabBarController in my app, I cannot get the first item in it's TabBar to be selected (i.e. blue color). It just stays black and gray.

Is there another method to activate a certain item in the TabBar? I'm using:

self.tabController.selectedIndex = 0;

(For matter of interested, this is all instantiated in the AppDelegate as the delegate is handling a welcome view) which is, once dismissed removed from screen and the UITabController.view is taking over.

Update:

In the Main App Delegate, I use this code to show the tabController and set the index:

#pragma mark - Click handlers

- (void) startApp:(id)sender {
    [UIView transitionFromView:self.welcomeView
                        toView:self.tabController.view
                      duration:0 
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    completion:nil];

    self.tabController.selectedIndex = 0;
}

self.tabController is not null, else I would not be able to see the view on the screen.

Upvotes: 3

Views: 2114

Answers (2)

Keith
Keith

Reputation: 46

This was happening to me as well. I finally realized it came down to the way I was constructing the UITabBarItems for my tab views. Being unable to find any examples of the proper way to provide the images for the tabs, I had settled on overridding tabBarItem in each of my views to return a UITabBarItem I had constructed myself. This appeared to load the icons properly but wouldn't change the higlight when I changed the selectedIndex programmatically.

Finally last night I found code like this in the init method of the child view controllers in an iOS book on Google Books:

UIImage *anImage = [UIImage imageNamed:@"foo"];
self.tabBarItem.image = anImage;

Setting up the tab bar items this way fixed the problem with highlighting the correct button when switching tabs programmatically for me.

Upvotes: 3

Rayfleck
Rayfleck

Reputation: 12106

Is self.tabController nil?

  NSLog(@" my tab controller is 0x%x", self.tabController);

Upvotes: 1

Related Questions