2

我有一个标签栏应用程序,它必须根据用户的偏好以不同的语言显示,但我找不到太多关于如何在运行时更改标签名称的信息。我需要标签在启动时显示正确的名称,而不是在访问标签时。

我能找到的最好的信息是跑步

self.tabBarController.selectedIndex = 1;
tabBarController.selectedViewController.tabBarItem.title = @"Tab Name";

来自应用程序委托,但这首先使选项卡处于活动状态,然后设置名称。

没有更好的方法在运行时设置选项卡名称吗?理想情况下,我想一口气将它们全部设置好。

4

5 回答 5

2

2个回答对我不起作用,我最终这样做了:

// Create temp strings to hold tab names
NSString *tab0Name;
NSString *tab1Name;
NSString *tab2Name;
NSString *tab3Name;
NSString *tab4Name;

// Set strings according to language
if ([UIAppDelegate.iStegAppLanguage isEqualToString:@"FR"])
{
    tab0Name = @"Accueil";
    tab1Name = @"Produits";
    tab2Name = @"Caisse";
    tab3Name = @"Branches";
    tab4Name = @"Plus";
}
else if ([UIAppDelegate.iStegAppLanguage isEqualToString:@"IT"])
{
    tab0Name = @"Home";
    tab1Name = @"Prodotti";
    tab2Name = @"Checkout";
    tab3Name = @"Filiali";
    tab4Name = @"More";
}
else if ([UIAppDelegate.iStegAppLanguage isEqualToString:@"EN"])
{
    tab0Name = @"Home";
    tab1Name = @"Products";
    tab2Name = @"Checkout";
    tab3Name = @"Branches";
    tab4Name = @"More";
}
else    // Default to german unless specifically set to another language
{
    tab0Name = @"Home";
    tab1Name = @"Produkte";
    tab2Name = @"Checkout";
    tab3Name = @"Filialen";
    tab4Name = @"Mehr";
}

// Set tab name
self.tabBarController.selectedIndex = 1;
tabBarController.selectedViewController.tabBarItem.title = tab1Name;
self.tabBarController.selectedIndex = 2;
tabBarController.selectedViewController.tabBarItem.title = tab2Name;
self.tabBarController.selectedIndex = 3;
tabBarController.selectedViewController.tabBarItem.title = tab3Name;
self.tabBarController.selectedIndex = 4;
tabBarController.selectedViewController.tabBarItem.title = tab4Name;
self.tabBarController.selectedIndex = 0;
tabBarController.selectedViewController.tabBarItem.title = tab0Name;    // Home last so it's shown first
于 2010-07-05T13:04:22.487 回答
2

如果您有对 UITabBar 的引用,则可以使用以下内容:

for (UITabBarItem *tabBarItem in tabBar)
{
  tabBarItem.title = NSLocalizedString(...);
}
于 2010-06-22T07:36:49.823 回答
1

I suggest you to localize your XIB file:

  • Right click on your XIB file
  • "Get Info"
  • "General" tab on the top
  • "Make File Localizable" button in the bottom
  • Back to "General" tab on the top
  • "Add Localization" button in the bottom + enter the locale you want (e.g. "en", "fr", "he", "ru" etc.)

Repeat the last step until you have all the requested languages.
I prefer to use "en" instead of the default "English" that is created automatically - if you prefer "en" too then delete the "English" in the end...

Now you can enter different titles to the tabs for each locale...

于 2010-06-22T08:08:20.573 回答
1

我这样做的方式是在应用程序委托中定义插座:

IBOutlet UITabBarItem *tabBarItem1;
IBOutlet UITabBarItem *tabBarItem2;
IBOutlet UITabBarItem *tabBarItem3;
IBOutlet UITabBarItem *tabBarItem4;

然后,在连接 IB 的插座后,将其放入- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

[tabBarItem1 setTitle:NSLocalizedString(@"tab1", @"")];
[tabBarItem2 setTitle:NSLocalizedString(@"tab2", @"")];
[tabBarItem3 setTitle:NSLocalizedString(@"tab3", @"")];
[tabBarItem4 setTitle:NSLocalizedString(@"tab4", @"")];

它可以工作,但我对此也不满意 - 由于某种原因,我无法让可本地化的 MainWindow.xib 正常工作..

于 2010-09-30T12:27:29.140 回答
0

我在用着:

NSArray *itemsTabBar = [[NSArray alloc] initWithArray:[self.tabBarController.tabBar items]];

    [[itemsTabBar objectAtIndex:0] setTitle:@"Contacts"];
    [[itemsTabBar objectAtIndex:1] setTitle:@"Settings"];
于 2012-08-01T03:05:06.013 回答