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?
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.
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"]];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With