在 apps.Delegate Objective C 全局更改标签栏标题

Change the Tab Bar title globally at apps.Delegate Objective C

如何在应用程序委托中更改标签栏项目标题。目前我有 5 个标签栏项目。如何在启动时以编程方式更改 5 个选项卡栏项目标题?

我的tab bar controller设置如下。我有 5 个 tab bar controller,其中 4 个嵌入 navigation controller。然后我有一个独立的 ViewController

请帮忙。

根据 Atanu Mondal 的回答更新

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[self setUpTabBar];

[self setUpTabBarSettings];

} 

-(void)setUpTabBar{

    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    tabBarController.tabBar.barTintColor = [UIColor redColor];
    tabBarController.tabBar.tintColor = [UIColor yellowColor];

    NSMutableArray *arrControllers=[NSMutableArray array];

    [arrControllers addObject:[self navControlWithRootVC:1]];
    [arrControllers addObject:[self navControlWithRootVC:2]];
    [arrControllers addObject:[self navControlWithRootVC:3]];
    [arrControllers addObject:[self navControlWithRootVC:4]];
    [arrControllers addObject:[self navControlWithRootVC:5]];

    [tabBarController setViewControllers:arrControllers];
    [self.window setRootViewController:tabBarController];

    [self.window makeKeyAndVisible];

}

-(UINavigationController *)navControlWithRootVC:(int)index{

    id viewController_;

    switch (index) {
        case 1:
            viewController_=[[UIViewController alloc] initWithNibName:@"ViewController1" bundle:nil];
            break;
        case 2:
            viewController_=[[UIViewController alloc] initWithNibName:@"ViewController" bundle:nil];
            break;
        case 3:
            viewController_=[[UIViewController alloc] initWithNibName:@"ViewController3:" bundle:nil];
            break;
        case 4:
            viewController_=[[UIViewController alloc] initWithNibName:@"ViewController4" bundle:nil];
            break;
        case 5:
            viewController_=[[UIViewController alloc] initWithNibName:@"ViewController5" bundle:nil];
            break;
    default:
            viewController_=[[UIViewController alloc] init];
            break;
            }
                                            
    UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:viewController_];

    return navControl;
                                            
}
                                            
-(void)setUpTabBarSettings{

    tabBar = self.tabBarController.tabBar;

    tabBarItem1 = [tabBar.items objectAtIndex:0];
    tabBarItem2 = [tabBar.items objectAtIndex:1];
    tabBarItem3 = [tabBar.items objectAtIndex:2];
    tabBarItem4 = [tabBar.items objectAtIndex:3];
    tabBarItem5 = [tabBar.items objectAtIndex:4];

   tabBarItem1.title = @"Title 1";

   tabBarItem2.title = @"Title 2";

   tabBarItem3.title = @"Title 3";

   tabBarItem4.title = @"Title 4";

   tabBarItem5.title = @"Title 5";

}

在哪里设置 ViewController 1,2,3,4,5?

试试这个

//in AppDelegate.h file 
UITabBar *tabBar;
UITabBarItem *tabBarItem1;
UITabBarItem *tabBarItem2;
UITabBarItem *tabBarItem3;
UITabBarItem *tabBarItem4;
UITabBarItem *tabBarItem5;

@property(nonatomic,strong) UITabBarController *tabBarController;

//in AppDelegate.m file 


-(void)setUpTabBar{

 self.tabBarController = [[UITabBarController alloc] init];    
 self.tabBarController.tabBar.barTintColor = [UIColor redColor];
 self.tabBarController.tabBar.tintColor = [UIColor yellowColor];  

 NSMutableArray *arrControllers=[NSMutableArray array];

 [arrControllers addObject:[self navControlWithRootVC:1]];
 [arrControllers addObject:[self navControlWithRootVC:2]];
 [arrControllers addObject:[self navControlWithRootVC:3]];
 [arrControllers addObject:[self navControlWithRootVC:4]];
 [arrControllers addObject:[self navControlWithRootVC:5]];

 [tabBarController setViewControllers:arrControllers];    
 [self.window setRootViewController:tabBarController];

 [self.window makeKeyAndVisible];

}

-(UINavigationController *)navControlWithRootVC:(int)index{
id viewController_;

 switch (index) {
     case 1:
        viewController_=[[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil];
        break;
     case 2:
        viewController_=[[ViewController2 alloc] initWithNibName:@"ViewController2” bundle:nil];
        break;
     case 3:
        viewController_=[[ViewController3 alloc] initWithNibName:@"ViewController3” bundle:nil];
        break;
     case 4:
        viewController_=[[ViewController4 alloc] initWithNibName:@"ViewController4” bundle:nil];
        break;
     case 5:
        viewController_=[[ViewController5 alloc] initWithNibName:@"ViewController5” bundle:nil];
        break;
     default:
        viewController_=[[UIViewController alloc] init];
        break;
}

UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:viewController_];    
return navControl;

}

-(void)setUpTabBarSettings{
 tabBar = self.tabBarController.tabBar; 
 tabBarItem1 = [tabBar.items objectAtIndex:0];
 tabBarItem2 = [tabBar.items objectAtIndex:1];
 tabBarItem3 = [tabBar.items objectAtIndex:2];
 tabBarItem4 = [tabBar.items objectAtIndex:3];
 tabBarItem5 = [tabBar.items objectAtIndex:4];

 tabBarItem1.selectedImage = [[UIImage imageNamed:@"img1_hover"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
 tabBarItem1.image = [[UIImage imageNamed:@"img1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
 tabBarItem1.title = @“Title 1”;


 tabBarItem2.selectedImage = [[UIImage imageNamed:@"img2_hover"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
 tabBarItem2.image = [[UIImage imageNamed:@"img2"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
 tabBarItem2.title = @“Title 2”;

 tabBarItem3.selectedImage = [[UIImage imageNamed:@"img3_hover"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
 tabBarItem3.image = [[UIImage imageNamed:@"img3"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
 tabBarItem3.title = @“Title 3”;

 tabBarItem4.selectedImage = [[UIImage imageNamed:@"img4_hover"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
 tabBarItem4.image = [[UIImage imageNamed:@"img4"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
 tabBarItem4.title = @“Title 4”;

 tabBarItem5.selectedImage = [[UIImage imageNamed:@"img5_hover"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
 tabBarItem5.image = [[UIImage imageNamed:@"img5"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
 tabBarItem5.title = @“Title 5”;

}
/* 
  Localizable_en.strings
*/
"NEWS" = "NEWS";

"CLASS" = "CLASS";

"GYM" = "GYM";

"SEARCH" = "SEARCH";

"MORE" = "MORE";

"CHANGE LANGUAGE" = "CHANGE LANGUAGE";


/* 
  Localizable_en.strings

*/
"NEWS" = "新闻";

"CLASS" = "课程";

"GYM" = "健身房";

"SEARCH" = "搜索";

"MORE" = "更多";

"CHANGE LANGUAGE" = "更改语言";

AppsDelegate

+ (NSString*)getCurrentLang {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *sLanguage = [defaults objectForKey:@"txtLanguage"];
    if(sLanguage == nil) {
        return @"EN";
    }else{
        return sLanguage;
    }
}

+ (NSString*)getLocalizedTableName {
    return [NSString stringWithFormat:@"Localizable_%@",[[self getCurrentLang]lowercaseString]];
}

+ (NSString*)getLocalizedText:(NSString*)toLocalize {
    return NSLocalizedStringFromTable(toLocalize, [AppDelegate getLocalizedTableName], @"");
}


 - (void)setupTabBar {

    UITabBarController * tabBarController = (UITabBarController*)[self.window rootViewController];

    if(tabBarController != nil) {
        ((UIViewController*)[tabBarController.viewControllers objectAtIndex:0]).tabBarItem.title = [AppDelegate getLocalizedText:@"CLASS"];
    ((UIViewController*)[tabBarController.viewControllers objectAtIndex:1]).tabBarItem.title = [AppDelegate getLocalizedText:@"NEWS"];
        ((UIViewController*)[tabBarController.viewControllers objectAtIndex:2]).tabBarItem.title = [AppDelegate getLocalizedText:@"GYM"];
        ((UIViewController*)[tabBarController.viewControllers objectAtIndex:3]).tabBarItem.title = [AppDelegate getLocalizedText:@"SEARCH"];
        ((UIViewController*)[tabBarController.viewControllers objectAtIndex:4]).tabBarItem.title =  [AppDelegate getLocalizedText:@"MORE"];
    }

}

ChangeLanguage.m

- (IBAction)btnEnglish:(id)sender {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:@"EN" forKey:@"txtLanguage"];

    [(AppDelegate*)[UIApplication sharedApplication].delegate setupTabBar];

    UITabBarController * tabBarController = (UITabBarController*)[[[UIApplication sharedApplication] keyWindow] rootViewController];
    tabBarController.selectedIndex = 0;
}