ios - TabBar 项目本地化

标签 ios tabbar

要本地化我的标签栏项目,我在 AppDelegate 中使用以下代码:

self.tabBarController = (UITabBarController*)self.window.rootViewController;
tabBarController.delegate = self;
tabBarController.selectedIndex = 0;
[[tabBarController.tabBar.items objectAtIndex:0] setTitle:NSLocalizedString(@"Home", nil)];
[[tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(@"Requests", nil)];
[[tabBarController.tabBar.items objectAtIndex:2] setTitle:NSLocalizedString(@"Account", nil)];
[[tabBarController.tabBar.items objectAtIndex:3] setTitle:NSLocalizedString(@"Alarms", nil)];
[[tabBarController.tabBar.items objectAtIndex:4] setTitle:NSLocalizedString(@"Settings", nil)];

现在我添加了另一个项目并尝试添加一个新行,索引 = 5,但我得到一个“NSRangeException”,因为索引 5 超出了范围。 Xcode 自动添加了“更多”部分并将我的最后两项(设置和新项)移到那里。 我还看到用户现在可以自定义选项卡栏来选择项目顺序。 那么现在我怎样才能引用所有项目并将它们本地化呢? 注意:我正在使用 Storyboard。

谢谢。

最佳答案

对我来说最简单的解决方案是实现 awakeFromNib 方法,即使在使用标签时也会运行。

// Objective-C
- (void)awakeFromNib{
    [super awakeFromNib];
    self.title = NSLocalizedString(@"YOUR-LOCALISATION-TAG", @"");
}


// Swift
override  func awakeFromNib() {
    super.awakeFromNib()
    self.title = NSLocalizedString("YOUR-LOCALISATION-TAG", comment: "")
}

关于ios - TabBar 项目本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12527161/

相关文章:

swift - 单击选项卡栏图标时重置 ViewController

javascript - 如何让指针事件在 iOS 中工作

ios - Apple 推送通知在生产环境中不起作用

iOS 标签栏图标模糊

flutter - 从 TabBarView Child 导航到另一个 Route(全屏)

ios - 如何设置滑动标签栏的y位置稍微向下

swift - SwiftUI 中堆栈隐藏在选项卡栏后面

ios - Obj-C/Swift 项目中的致命陷阱异常

iphone - 使用方形 setBackgroundImage 的圆角 UIButton

ios - 持久化 Collection View 单元格选择