Hanz Cheah
Hanz Cheah

Reputation: 811

Set Tab Bar title programatically at Startup for 5 Tab Bar item, 4 of which is embedded in Navigation Controller, 1 is not. Objective C

How to set the tab bar title at Startup or AppDelegates. I have 5 Tab Bar Item, 4 of which is embedded in a Navigation Controller and 1 is without and just a tab bar item. Please see the following pic

Pic

Updated on the VC calling the method

#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];
 }
    //====================================

Update on app 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";
     }

Upvotes: 1

Views: 466

Answers (1)

Reinier Melian
Reinier Melian

Reputation: 20804

Any UIViewController have this property tabBarItem because is an extension of UIViewController so you only have to get your viewController and set his tabBarItem.title property = "your desiredTitle"

UPDATE

Objective-C Code

- (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 Code

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"
       }
   }
}

UPDATE #2

if you want to call this method from anywhere in your code you must

  1. import your AppDelegate.h in the .m where you want to use it
  2. add this method in your AppDelegate.h in order to make this method public
  3. call this method like this:

Code

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

enter image description here

Upvotes: 2

Related Questions