Измените заголовок панели вкладок глобально в приложениях. Задайте цель C - PullRequest
0 голосов
/ 27 апреля 2018

Как изменить заголовок элемента панели вкладок у делегата приложения. В настоящее время у меня есть 5 элементов панели вкладок. Как программно изменить заголовок элемента панели 5 вкладок при запуске?

Мой tab bar controller настроен следующим образом. У меня 5 tab bar controller и 4 из которых встроены в navigation controller. Тогда у меня есть один, который является автономным ViewController

Пожалуйста, помогите.

please upload

Обновлено в соответствии с ответом Атану Мондала

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

  1. Pic 1 или

  2. Pic 2

Ответы [ 2 ]

0 голосов
/ 18 июля 2018
/* 
  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;
}
0 голосов
/ 27 апреля 2018

Попробуйте с этим

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

}
...