UITabbarController更改栏项目标题以编程方式
我试图以编程方式更改我的应用程序中的选项卡栏项目的名称。UITabbarController更改栏项目标题以编程方式
在我故事板我有一个的UITabBarController设置为初始视图和层次结构如下:
的UITabBarController - >的UINavigationController - >的UITableViewController - >详细视图控制器
要改变您需要更改UINavigationController的条形图项目的标题。在IB中一切正常,但我需要本地化我的应用程序,这是我动态执行的(无需重新启动应用程序),每次启动应用程序时,IB中给出的应用程序标题都会设置,并且标题不会更改为它们的标题本地化的标题,直到我点击酒吧项目。
在我各自的UITableViewControllers中,我使用self.title = localized title;
来设置标题,效果很好。 但是我希望应用程序在启动时更改条款项目标题,直到我点击它们。
我在这里看过关于这个问题的帖子,并尝试了这些建议,但仍然将酒吧项目的标题从IB设置为它们的值。我也尝试下面的代码,但只在启动时设置选中的栏:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Select the desired tab of our initial tab bar controller:
UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 1;
tabBar.navigationController.title = @"Item 2";
[(UITabBarItem*)[self->tabBarController.tabBar.items objectAtIndex:1] setTitle:@"Item 2"];
// Override point for customization after application launch.
return YES;
}
好吧,我正在回答我自己的问题。经过了很长时间的网络搜索之后,我尝试了各种各样的组合。下面的后帮我弄的轨道上,但是它并没有解决它(仍然给它+1):
Tab Bar Item title before appear Storyboard
在的UITableViewController .m文件,你需要添加此代码设置的标题在加载或点击之前的标签栏项目。
- (id)initWithCoder:(NSCoder *)decoder {
self = [super initWithCoder:decoder];
if (self) {
self.navigationController.tabBarItem.title = NSLocalizedStringFromTableInBundle(@"TITLE_TABLANGUAGE", nil, currentLanguageBundle, @"");
}
return self;
}
答案就在该的UITabBarController的物品栏得到他们的价值观,在这种情况下,一个标题,从UINavigationController的酒吧项目称号。在initWithCoder方法中设置该标题非常重要。把它放在其他地方不会在它被点击之前设置标题。
我认为你让它比它需要更困难。要更改标签栏项目的标题在运行时,简单地做从维持在问题选项卡中的视图 - 控制以下内容:
self.navigationController.tabBarItem.title = @"desired title text";
完成。
使用此功能直接:
private func updateTabBarTitles() {
if let navControllers = viewControllers as? [UINavigationController] {
let vcs = navControllers.map({$0.viewControllers.first!})
for vc in vcs {
switch vc {
case is First: vc.navigationController?.tabBarItem.title = "firstVCTabbarTitle"
case is Second: vc.navigationController?.tabBarItem.title = "secondVCTabbarTitle"
default: break
}
}
}
}