开发者

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

This application I'm writing has a problem.

I'm setting up the UITabBar in my application window and set the icons in the view files. But when i run the app, the first icons show up (because the view is loaded I guess) and the other icons do not show up until I click them.

Do i need to implement self.tabBarItem in some other method not viewDidLoad?

Thanks in advance to everyone!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    tabBar = [[UITabBarController alloc] init];

    SubscriptionsController *subscriptionsController = [[SubscriptionsController alloc] init];
    FavoritesController  *favoritesController  = [[FavoritesController  alloc] init];
    CategoriesController *categoriesController  = [[CategoriesController alloc] init];
    TagsController   *tagsController    = [[TagsController   alloc] init];
    HelpScreenController *helpScreenController  = [[HelpScreenController alloc] init];

    tabBar.viewControllers = [NSArray arrayWithObjects:
        subscriptionsController, 
        favoritesController, 
        categoriesContro开发者_运维问答ller, 
        tagsController, 
        helpScreenController, 
        nil
        ];

    [window addSubview:tabBar.view];

    // Override point for customization after application launch.
    [window makeKeyAndVisible];
    return YES;
}

//The View

- (void)viewDidLoad {
    [super viewDidLoad];
    tabIcon = [[UITabBarItem alloc] initWithTitle:@"Abonime" image:[UIImage imageNamed:@"subscr.png"] tag:0];
    self.tabBarItem = tabIcon;
    [tabIcon release];
}


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜