2014-03-29 211 views
1

使用UITabBarController,我遇到了以下問題。 我想在每個選項卡中有一個給定的字符串作爲我所有視圖控制器的標題。這顯然不可能在storyboard內設置。以編程方式在UITabBarController中設置標題

通過搜索我發現這個幾乎可行的解決方案。在每個單獨的視圖控制器中放入以下類型的代碼。

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    UITabBarItem *tabBarItem; 
    tabBarItem=[[UITabBarItem alloc] initWithTitle:nil 
              image:[self imageFromText:@"My Tab Title" 
                   withFont:[UIFont systemFontOfSize:17.0]] tag:0]; 
    self.tabBarItem=tabBarItem; 
} 

問題是:所有的標題只設置爲我想要的只有當用戶點擊所有標籤。

這當然不是一個好的永久性解決方案。我需要在應用程序啓動時將所有標題正確顯示。

任何人有一個想法如何做到這一點?

+0

我希望可以幫助你。 –

回答

0

您可以在AppDelegate中更改它。

例如如果你有3 TabBarIcons

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; 

    [[tabBarController.viewControllers objectAtIndex:0] setTitle:@"Your "]; 
    [[tabBarController.viewControllers objectAtIndex:1] setTitle:@"title "]; 
    [[tabBarController.viewControllers objectAtIndex:2] setTitle:@"here"]; 

    [[[tabBarController.viewControllers objectAtIndex:0] tabBarItem] setImage:[[UIImage imageNamed:@"first"] imageWithRenderingMode:UIImageRenderingModeAutomatic]]; 
    [[[tabBarController.viewControllers objectAtIndex:1] tabBarItem] setImage:[[UIImage imageNamed:@"second"] imageWithRenderingMode:UIImageRenderingModeAutomatic]]; 
    [[[tabBarController.viewControllers objectAtIndex:2] tabBarItem] setImage:[[UIImage imageNamed:@"third"] imageWithRenderingMode:UIImageRenderingModeAutomatic]]; 

    return YES; 
} 
+1

我最終通過繼承UITabBarController。但是我所做的和你的建議很相似。 – Michel

相關問題