UITabBarItems in UITabBar show after I click the item not when application launches

守給你的承諾、 提交于 2019-12-05 00:59:46

I think you should set the tabBarItem property in a view controller's designated initializer (judging from your code, it must be -init for each of the controllers). In fact, the tab bar controller is smart enough to load the views on demand, that is, the tabBarItem property should be set before viewDidLoad gets sent.

Also, you seem to be leaking all the view controllers. To fix that, do the following:

SubscriptionsController *subscriptionsController = [[[SubscriptionsController alloc] init] autorelease];

Correct. The icons don't show up because the view (other than the first one, isn't loaded yet). And doesn't get loaded until you tap a view because viewDidLoad isn't called until then.

Remove the code in the individual UIViewControllers viewDidLoad and Do this...

NSArray *controllers = [NSArray arrayWithObjects:
                                                [NSDictionary dictionaryWithObjectsAndKeys:@"SubscriptionsController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
                                                [NSDictionary dictionaryWithObjectsAndKeys:@"FavoritesController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
                                                [NSDictionary dictionaryWithObjectsAndKeys:@"CategoriesController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
                                                [NSDictionary dictionaryWithObjectsAndKeys:@"TagsController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
                                                [NSDictionary dictionaryWithObjectsAndKeys:@"HelpScreenController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil],
                                                nil];

NSMutableArray *controllerArray = [NSMutableArray array] ;

 for (NSUInteger i = 0; i < [controllers count]; i++)
 {
    id newClass = [[NSClassFromString([[controllers objectAtIndex:i] objectForKey:@"class"]) alloc] init];
    UITabBarItem *tabItem = [[UITabBarItem alloc] init];
    tabItem.image = [[controllers objectAtIndex:i] objectForKey:@"icon"];
    tabItem.title = [[controllers objectAtIndex:i] objectForKey:@"title"];
    tabItem.tag = i;
    [(UIViewController*)newClass setTabBarItem:tabItem];
    [tabItem release];
    [controllerArray addObject:newClass];
    [newClass release];
 }

 tabBar.viewControllers = controllerArray;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!