2010-12-11 12 views
11

このアプリケーションは問題があります。UITabBarItems in UITabBarは、アプリケーションが起動したときにアイテムをクリックしないと表示されます。

私はアプリケーションウィンドウに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]; 
} 

答えて

12

私はあなたが(あなたのコードから判断し、それは各コントローラの-initでなければならない)、View Controllerの指定イニシャライザでtabBarItemプロパティを設定するべきだと思います。実際、タブバーコントローラはスマートであり、必要に応じてビューをロードすることができます。つまり、tabBarItemプロパティはviewDidLoadが送信される前に設定する必要があります。

また、すべてのView Controllerがリークしているようです。これを修正するには、次の操作を行います。

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

どうもありがとう! :)それは完璧に働いた:) – Olsi

+0

私は迅速な2でこれを行うには? – deepakssn

+0

これはまだ7年後のSwiftの問題を解決します。 – SQLiteNoob

4

正しい。ビュー(最初のビュー以外はまだ読み込まれていないため)は表示されません。それまでviewDidLoadが呼び出されていないので、ビューをタップするまではロードされません。

個々UIViewControllersのviewDidLoadでコードを削除し、これを行ってください...

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; 
関連する問題