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

我能找到的最好的信息正在运行

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

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

是否没有更好的方法在运行时设置标签名称?理想情况下,我想一次将它们全部设置。

有帮助吗?

解决方案 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

其他提示

如果您对Uitabbar有参考,则可以使用类似的内容:

for (UITabBarItem *tabBarItem in tabBar)
{
  tabBarItem.title = NSLocalizedString(...);
}

我建议您本地化XIB文件:

  • 右键单击您的XIB文件
  • “获取信息”
  • 顶部的“常规”标签
  • 底部的“使文件可本地化”按钮
  • 返回顶部的“常规”选项卡
  • 底部中的“添加本地化”按钮 +输入所需的语言环境(例如“ en”,“ fr”,“ He”,“ ru”等)

重复最后一步,直到您拥有所有请求的语言。
我更喜欢使用“ en”而不是自动创建的默认“英语” - 如果您也喜欢“ en”,则最终删除“英语” ...

现在,您可以输入每个语言环境的选项卡的不同标题...

我这样做的方式是定义应用程序委托中的媒体:

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正常工作。

我在用着:

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

    [[itemsTabBar objectAtIndex:0] setTitle:@"Contacts"];
    [[itemsTabBar objectAtIndex:1] setTitle:@"Settings"];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top