views:

2389

answers:

3

I am adding a viewController to a TabBarController. When I add a ViewController from the custom class and Nib, it''s icon does not show up in the tabBar.

If I initialize like this the icon does not show up.

viewController = [[FlashCardViewController alloc] initWithNibName:@"FlashCardViewController"  bundle:[NSBundle mainBundle]];

But creating a generic viewController works.

viewController = [[UIViewController alloc] initWithNibName:nil  bundle:nil];

Here we add the image and title.

viewController.title = @"Quiz";
viewController.tabBarItem.image = [UIImage imageNamed:@"magnifying-glass.png"];

How can I get the icon to display if load from a NIB?

+1  A: 

You can add the call to the tabBarItem.image setter inside the custom view controller's viewDidLoad method:

@implementation FlashCardViewController
//...
- (void)viewDidLoad {
    [super viewDidLoad];

    self.tabBarItem.image = [UIImage imageNamed:@"magnifying-glass.png"];
}
//...
@end

Edit: OK, so that didn't work. Try:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed:@"magnifying-glass.png"];
    self.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"string"
                                                     image:image
                                                       tag:0] autorelease];
}
Tim
I thought for sure that your suggestion would work, but still no icon. Any other ideas Tim?
Bryan
Maybe initialize the entire tab bar item yourself? See edited code above.
Tim
Thanks. I was overwriting the image and title in another spot. This actually works! Only one problem here is that until you click on the tab to load the view, the tabBarItem has no icon or label. Any ideas?
Bryan
Move the code from `viewDidLoad` into `initWithNibName:bundle:`?
Tim
Ahh! I will try that. Thanks.
Bryan
A: 

Why are you passing in [NSBundle mainbundle] to the FlashCardViewController init? Usually you just pass in nil - as per your working example...

Kendall Helmstetter Gelner
I thought that might be a problem and I switched it to nil but there was no difference so I stuck with what I had before. I had been loading FlashCardViewController by itself - not into a TabBarController - and it worked great.I am not sure why I pass NSBundle mainbubdle. What does that specify?
Bryan
A: 

Thanks! I didn't know I can change it from here. You are like god for me now :P

victorvj