简体   繁体   中英

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?

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"]];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM