0

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

Community
  • 1
  • 1
Hanz Cheah
  • 761
  • 5
  • 15
  • 44

2 Answers2

0

Try with this

//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”;

}
Atanu Mondal
  • 1,714
  • 1
  • 24
  • 30
  • Property 'tabBarController' not found on object of type 'AppDelegate – Hanz Cheah Apr 27 '18 at 08:20
  • Do i need to initialise UITabBarController? – Hanz Cheah Apr 27 '18 at 08:21
  • use self.tabBarController – Atanu Mondal Apr 27 '18 at 08:47
  • Can you elaborate more, don't get what you trying to say, sorry. – Hanz Cheah Apr 27 '18 at 08:54
  • use UITabBar *tabBar = self.tabBarController.tabBar, to get the main tab-bar. – Atanu Mondal Apr 27 '18 at 08:59
  • Add @property(nonatomic,strong) UITabBarController *tabBarController; in AppDelegate.h file – Atanu Mondal Apr 27 '18 at 09:25
  • Called the method at `(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions` and not working. I have cleared my title and image at IB. – Hanz Cheah Apr 27 '18 at 09:37
  • first call [self setUpTabBar]; in (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions . After that call [self setUpTabBarSettings]; Updated the answer. – Atanu Mondal Apr 27 '18 at 09:58
  • I have tried your updated answer but my application crashed with the following error. `''NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'ViewController1''` I have changed your updated answer abit but not sure where exactly to set the ViewController title, Please help, gladly appreciated. – Hanz Cheah Apr 30 '18 at 02:11
  • You have used the StoryBoard. So, create the viewController object accordingly. In my answer i have used .xib file. Modify `-(UINavigationController *)navControlWithRootVC:(int)index` this method, when you are creating viewController object . – Atanu Mondal Apr 30 '18 at 11:08
0
/* 
  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;
}
Hanz Cheah
  • 761
  • 5
  • 15
  • 44