Search code examples
iosobjective-cuinavigationcontrolleruitabbaritem

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


How to change the tab bar item title at apps delegate. Currently I have 5 tab bar item. How to change the 5 tab bar item title programmatically at startup?

My tab bar controller is set up as follow. I have 5 tab bar controller and 4 of which is embedded in a navigation controller. Then I have one which is a standalone ViewController

Please help.

please upload

Updated according to Atanu Mondal answer

- (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";

}

Where to set the ViewController 1,2,3,4,5?

  1. Pic 1 or

  2. Pic 2


Solution

  • /* 
      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;
    }