Objective-C:标签栏项目的标题在登录页面后不会改变,但在从xCode重新启动时有效吗?

问题描述 投票:0回答:1

我有一个改变应用程序语言的Change Language VC。如果我最初运行应用程序,注册并登录。然后我立即去change language VC并点击change language method。标签栏项目的标题仍将保留,不会触发。请查看链接Please focus on the title of the tab bar item

但是,如果在xCode中重新启动应用程序并且它位于初始页面或Home VC而不是登录页面并且我转到change language VC,一切正常。请查看链接和Please focus on the title of the tab bar item

对于那里的所有大师,请帮助我,我想要更改标签栏项目的标题。谢谢。

StoryBoard

StoryBoard

Localized.cn.strings

"Home" = "主页";

"News" = "新闻";

"Class" = "课程";

"Gym" = "健身房";

"More" = "更多";

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

LoginVC

- (void)dismissLoginAndShowProfile {

    AppDelegate *authObj = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    authObj.authenticated = YES;

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITabBarController *tabView = [storyboard instantiateViewControllerWithIdentifier:@"profileView"];
    tabView.selectedIndex=0; //=== This is to choose which Tab, starts with 0,1,2,3,4
    [self presentViewController:tabView animated:YES completion:nil];
}

AppsDelegate.m (AppsDelegate will call the about table and get the title accordingly)

+ (NSString*)getCurrentLang {

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

//=== This method will getCurrentLang and concatanate with Localizable_cn or en
+ (NSString*)getLocalizedTableName {
    return [NSString stringWithFormat:@"Localizable_%@",[[self    getCurrentLang]lowercaseString]];
}

//=== This method will call getLocalizedTableName, 
+ (NSString*)getLocalizedText:(NSString*)toLocalize {
    return NSLocalizedStringFromTable(toLocalize, [AppDelegate getLocalizedTableName], @"");
}

//=== Change Language VC will call the following method
//=== the Method will can getLocatlizedText method and pass @"Home" to it
- (void)setupTabBar {

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

    if(tabBarController != nil) {
        ((UIViewController*)[tabBarController.viewControllers objectAtIndex:0]).tabBarItem.title = [AppDelegate getLocalizedText:@"Home"];
        ((UIViewController*)[tabBarController.viewControllers objectAtIndex:1]).tabBarItem.title = [AppDelegate getLocalizedText:@"News"];
        ((UIViewController*)[tabBarController.viewControllers objectAtIndex:2]).tabBarItem.title = [AppDelegate getLocalizedText:@"Class"];
        ((UIViewController*)[tabBarController.viewControllers objectAtIndex:3]).tabBarItem.title = [AppDelegate getLocalizedText:@"Gym"];
        ((UIViewController*)[tabBarController.viewControllers objectAtIndex:4]).tabBarItem.title =  [AppDelegate getLocalizedText:@"More"];
    }
}

ProfileChgLang.m (I only show the button Change to Chinese button method)

- (IBAction)btnChinese:(id)sender {

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

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

    UINavigationController *navigationController = self.navigationController;
    [navigationController popViewControllerAnimated:YES];
}
objective-c uinavigationcontroller uitabbar uitabbaritem
1个回答
0
投票

在LoginVC中使用以下方法。添加最后一行。

    - (void)dismissLoginAndShowProfile {

       AppDelegate *authObj = (AppDelegate*)[[UIApplication sharedApplication] delegate];
       authObj.authenticated = YES;

       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
       UITabBarController *tabView = [storyboard instantiateViewControllerWithIdentifier:@"profileView"];
       tabView.selectedIndex=0; //=== This is to choose which Tab, starts with 0,1,2,3,4
       [self presentViewController:tabView animated:YES completion:nil];

       [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil]; //==== This is the line that is needed
    }
© www.soinside.com 2019 - 2024. All rights reserved.