12

我正在编写的这个应用程序有问题。

UITabBar在我的应用程序窗口中设置并在视图文件中设置图标。但是当我运行该应用程序时,第一个图标会显示(因为我猜是视图已加载),而其他图标在我单击它们之前不会显示。

我不需要用self.tabBarItem其他方法实现viewDidLoad吗?

在此先感谢大家!

- (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, 
        categoriesController, 
        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];
}
4

2 回答 2

14

我认为您应该在视图控制器的指定初始化程序中设置 tabBarItem 属性(从您的代码来看,它必须-init针对每个控制器)。实际上,标签栏控制器足够聪明,可以按需加载视图,也就是说,应该在viewDidLoad发送之前设置 tabBarItem 属性。

此外,您似乎泄漏了所有视图控制器。要解决此问题,请执行以下操作:

SubscriptionsController *subscriptionsController = [[[SubscriptionsController alloc] init] autorelease];
于 2010-12-11T16:30:16.770 回答
4

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;
于 2010-12-11T18:25:47.873 回答