2010-12-11 44 views
11

我正在寫這個應用程序有一個問題。UITabBarItems 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

我想你應該設置在一個視圖控制器的指定初始化的tabBarItem屬性(從你的代碼來看,它必須是-init每個控制器)。實際上,標籤欄控制器足夠聰明,可以按需加載視圖,也就是說,應在發送viewDidLoad之前設置tabBarItem屬性。

此外,你似乎泄漏所有的視圖控制器。要解決該問題,請執行以下操作:

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

非常感謝! :)它的工作完美無缺:) – Olsi 2010-12-11 21:17:59

+0

我如何在swift 2中做到這一點? – deepakssn 2015-10-27 06:22:28

+0

這仍然解決了Swift中的問題 - 7年後。 – SQLiteNoob 2017-09-28 17:11:36

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; 
相關問題