在运行时重命名选项卡

发布于 2024-09-06 12:57:59 字数 356 浏览 3 评论 0原文

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

我能找到的最好的信息是

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

从应用程序委托运行,但这首先使选项卡处于活动状态并打开该选项卡。然后设置名称。

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

I have a tab bar app that has to display in different languages depending on a user's preference but I can't find much info on how to change the tab names at run-time. I need the tabs to show the correct names at startup, not when the tabs are accessed.

Best info I could find was running

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

from the app delegate but this first makes the tab active & then sets the name.

Is there not a better way to set tab names at run-time? Ideally I'd like to set them all in one go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜深人未静 2024-09-13 12:57:59

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

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

If you have a reference to the UITabBar, you can use something like:

for (UITabBarItem *tabBarItem in tabBar)
{
  tabBarItem.title = NSLocalizedString(...);
}
沉溺在你眼里的海 2024-09-13 12:57:59

这两个答案对我来说不起作用,我最终这样做了:

// 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

The 2 responses didn't work for me, I eventually did it like this:

// 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
追风人 2024-09-13 12:57:59

我建议您本地化您的 XIB 文件:

  • 右键单击​​您的 XIB 文件
  • “获取信息”
  • 顶部的“常规”选项卡 底部的
  • “使文件可本地化”按钮
  • 返回到顶部的“常规”选项卡
  • “添加本地化底部的“按钮 + 输入您想要的区域设置(例如“en”、“fr”、“he”、“ru”等)

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

现在您可以在每个区域设置的选项卡中输入不同的标题...

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...

再见回来 2024-09-13 12:57:59

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

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

The way I did it is by defining outlets in the app delegate:

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

and then, after connecting the outlets at IB, puting this to - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions :

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

It works but I'm not happy with that either - for some reason I can't get a localizable MainWindow.xib properly working..

一萌ing 2024-09-13 12:57:59

我正在使用:

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

    [[itemsTabBar objectAtIndex:0] setTitle:@"Contacts"];
    [[itemsTabBar objectAtIndex:1] setTitle:@"Settings"];

I'm using:

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

    [[itemsTabBar objectAtIndex:0] setTitle:@"Contacts"];
    [[itemsTabBar objectAtIndex:1] setTitle:@"Settings"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文