2012-04-10 79 views
3

我的應用程序以帶有5個選項卡的標籤欄控制器開始。在開始時第一個以其名稱呈現,但其他四個在我點擊它們之前沒有名稱。然後該名稱顯示取決於用戶擁有哪種語言。 如何在標籤欄出現之前設置標籤的名稱? 我正在使用故事板。有沒有一種方法可以通過編程方式在標籤欄中設置標題,其餘的都是通過storyboard完成的?我在AppDelegate嘗試了一些像[FirstViewController setTitle:NSLocalizedString(@"Titel1", nil)]; 但我得到一個錯誤,沒有選擇器setTitle的類方法。 謝謝。標籤欄項目標題出現之前Storyboard

回答

12

今天我有同樣的問題。我也使用故事板。在我的情況下,我用下面的方式。

  1. 我創建了一個新的「Objective-C類」的名稱 「MainTabBarViewController」作爲「的UITabBarController」的子類。
  2. 在我的「身份檢查員」故事板中,我將「自定義類」更改爲「MainTabBarViewController」。

  3. 在 「MainTabBarViewController」 方法 「viewDidLoad中的」 i加入:

    [[self.viewControllers objectAtIndex:0]的setTitle:NSLocalizedString(@ 「家」,零)];

    [[self.viewControllers objectAtIndex:1] setTitle:NSLocalizedString(@「statistics」,nil)];

    [[self.viewControllers objectAtIndex:2] setTitle:NSLocalizedString(@「settings」,nil)];

    [[self.viewControllers objectAtIndex:3] setTitle:NSLocalizedString(@「info」,nil)];

我想這不是完美的方式,但對我來說,它是完美的。

+1

嗨,那裏...我喜歡這個主意,你說得對,它完美的作品......非常感謝。 – halloway4b 2012-05-21 19:17:14

+0

我不敢相信!我一直在尋找這個解決方案這麼久。非常感謝你:) – thedp 2013-10-14 00:36:13

+0

請參閱下面的解決方案,它不需要使用索引設置標題。 – 2014-03-23 17:29:44

1

在應用程序的委託,要在其中創建視圖控制器,在這裏(而不是viewDidLoad)設置標題屬性,例如:

vc1 = [[VC1 alloc] init]; 
vc1.title = @"List"; 

vc2 = [[VC2 alloc] init]; 
vc2.title = @"Map"; 

tabBarController.viewControllers = [[NSArray alloc] initWithObjects:vc1, vc2, nil]; 
+0

我正在使用故事板。所以我在故事板中創建了它們。故事板中是否有另外一種方法來做到這一點? – halloway4b 2012-04-10 12:43:15

+0

我不知道,因爲我不使用故事板。 – 2012-04-10 13:20:17

+0

好的......謝謝.... – halloway4b 2012-04-10 13:44:41

0

如果你看看由Xcode中創建的FirstViewController使用基於在- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil方法模板標籤-Bar應用程序,你可以看到這樣的事情:

if (self) { 
    //check your language 
    self.title = NSLocalizedString(@"First", @"First"); 
    self.tabBarItem.image = [UIImage imageNamed:@"first"]; 
} 

所以,你必須檢查你的語言,然後設置title屬性,這將設置次標題欄和導航欄中的標題。

+0

我在Storyboard中有一個Tab Bar View Controller,當它顯示時,我希望顯示所有標籤欄標題,具體取決於用戶在iPhone中配置的語言。 – halloway4b 2012-04-10 13:46:22

0

試試這個,在每個視圖u必須使用此方法

假設這是你第一個視圖

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    if (self) { 
     self.title = NSLocalizedString(@"homepage", @"homepage"); 
     [self.tabBarItem setTag:0]; 
     self.tabBarItem.image = [UIImage imageNamed:@"homepage"]; 
    } 
    return self; 
} 

在您的應用程序委託類編寫代碼添加UITabBarController

self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3,viewController4,viewController5, nil]; 
self.window.rootViewController = self.tabBarController; 
[self.window makeKeyAndVisible]; 

加入上述代碼

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {} 
+0

這是否與故事板和弧線一起工作? – halloway4b 2012-04-10 13:11:11

+0

我不知道這個故事無聊,但它會在其他項目中挑釁 – freelancer 2012-04-10 13:13:54

2

我有兩個選項卡是這樣的結構:

MainTabBarController : UITabBarController 
    +- MessagesNavigationController : UINavigationController 
    +- ContactsNavigationController : UINavigationController 

與故事板和ARC的結構我在我的UITabBarController視圖控制器的自定義類重載了initWithCoder:選擇器(在這種情況下ContactsNavigationController)和初始化所述tabBarItem.title有這樣的:

@implementation ContactsNavigationController 

... 

- (id)initWithCoder:(NSCoder *)decoder 
{ 
    self = [super initWithCoder:decoder]; 
    if (self) { 
     self.tabBarItem.title = NSLocalizedString(@"Contacts", nil); 
    } 
    return self; 
} 

當使用故事板iOS的調用(id)initWithCoder:代替-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil當故事板視圖控制在UITabBarController啓動期間初始化ler。還要注意,viewDidLoad直到從選項卡中選擇視圖控制器選項卡纔會被調用。