Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITabBarController tab images and titles are not set until I tap them

I have set the properties title, image and selected-image of each of my UIViewControllers in their viewDidLoad: method. These View Controllers are in a UITabBarController, and according to the documentation provided, if you set these three properties, the UITabBarController will automatically display them.

[self setTitle: @"title"];
[self.tabBarItem setImage: [UIImage imageNamed:@"image.png"]];
[self.tabBarItem setSelectedImage:[UIImage imageNamed:@"image-selected.png"]];

But the problem is that the View Controllers are not instantiated until the user taps the corresponding tab in the UITabBar, so until the moment that the tabs are not pressed, neither the images or the title are shown.

Is there any clean and OOP way to make the UITabBarController refresh and pull the data from its View Controllers? What's the point of having a title, image and selected-image property if you have to manually tell the UITabBarController which is which?

like image 833
tomidelucca Avatar asked Sep 28 '14 10:09

tomidelucca


2 Answers

viewDidLoad is not called until the view actually needs to be loaded for the first time, which in this case is when you switch to the ViewController's tab.

Try setting these properties in the ViewController's designated initializer instead.

like image 100
mbo42 Avatar answered Oct 18 '22 19:10

mbo42


If you are using storyboard you should use awakeFromNib instead. Like this:

- (void)awakeFromNib {
    [self setTitle:@"title"];
    [self.tabBarItem setImage:[UIImage imageNamed:@"image.png"]];
    [self.tabBarItem setSelectedImage:[UIImage imageNamed:@"image-selected.png"]];
}
like image 20
turingtested Avatar answered Oct 18 '22 21:10

turingtested