如何在 Startup 或 AppDelegates
中设置标签栏标题。我有 5 个 Tab Bar Item
,其中 4 个嵌入在 Navigation Controller
中,1 个没有,只是一个 tab bar item
。请看下图
更新调用方法的VC
#import "AppDelegate.h"
@interface ProfileChgLang (){
AppDelegate *appDelegate;
NSString *sLanguage;
}
- (IBAction)btnChinese:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"CN" forKey:@"txtLanguage"];
[(AppDelegate*)[UIApplication sharedApplication].delegate setupTabBar];
//UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//==== Is the following correct? =====
UITabBarController * tabBarController = (UITabBarController*)[[[UIApplication sharedApplication] keyWindow] rootViewController];
[self presentViewController:tabBarController animated:YES completion:nil];
}
//====================================
应用委托(delegate)更新
- (void)setupTabBar {
//===Should be this
UITabBarController * tabBarController = (UITabBarController*)[self.window rootViewController];
//===Or this
UITabBarController * tabBarController = (UITabBarController*)[[[UIApplication sharedApplication] keyWindow] rootViewController];
if(tabBarController != nil) {
((UIViewController*)[tabBarController.viewControllers objectAtIndex:1]).tabBarItem.title = @"Your desired Title";
}
最佳答案
任何 UIViewController
都有这个属性 tabBarItem
因为它是 UIViewController
的扩展所以你只需要获取你的 viewController 并设置他的 tabBarItem.title
property = "your desiredTitle"
更新
Objective-C 代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setupTabBar];
// Override point for customization after application launch.
return YES;
}
- (void)setupTabBar {
UITabBarController * tabBarController = (UITabBarController*)[self.window rootViewController];
if(tabBarController != nil) {
((UIViewController*)[tabBarController.viewControllers objectAtIndex:3]).tabBarItem.title = @"YourDesiredTitle";
}
}
Swift 代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Whatever you have here
self.setupTabBar()
return true
}
func setupTabBar() {
if let tabBarController = self.window?.rootViewController as? UITabBarController {
if let navigationsControllers = tabBarController.viewControllers as? [UIViewController] {
navigationsControllers[3].tabBarItem.title = "YourTitle"
}
}
}
更新#2
如果你想从代码中的任何地方调用这个方法,你必须
- 将您的 AppDelegate.h 导入您要使用它的 .m 中
- 将此方法添加到您的 AppDelegate.h 中以公开此方法
- 像这样调用这个方法:
代码
[(AppDelegate*)[UIApplication sharedApplication].delegate setupTabBar];
关于ios - 在启动时以编程方式为 5 个选项卡栏项目设置选项卡栏标题,其中 4 个嵌入在导航 Controller 中,1 个不是。 objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50096039/