0

在我的应用程序中,我需要提供三种语言支持。我的默认语言是英语。一旦用户在设置控制器中选择了其他语言,整个应用程序应更改为该特定语言。为此,我决定创建一个包含所有三种语言及其数据的 plist 文件。现在我的默认语言是英语,这很好。现在我在 LanguageController 中使用 plist 更改了语言并推送到主控制器。我的家庭控制器未收到所选语言(法语)字符串。这是我的代码,

//LanguageController.h

NSMutableDictionary *allDictElements;
NSString *selectedLanguage1;
NSArray *dummy;

@property(nonatomic,retain) NSMutableDictionary *allDictElements;
@property(nonatomic,retain) NSString *selectedLanguage1;
@property(nonatomic,retain) NSArray *dummy;

 //LanguageController.m

@synthesize allDictElements,selectedLanguage1,dummy;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSString *path= [[NSBundle mainBundle] pathForResource:@"Language" ofType:@"plist"];

    allDictElements = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    NSLog(@"%@allDictElements",allDictElements);

    LeeValleyHomeViewController *homeController = [[LeeValleyHomeViewController alloc] initWithNibName:@"LeeValleyHomeViewController" bundle:[NSBundle mainBundle]];

     if (indexPath.row ==0)
     {
        //bookshelf button
        NSMutableDictionary *english=[[NSMutableDictionary alloc]init];
        english=[allDictElements objectForKey:@"English Language"];
        dummy=[english objectForKey:@"English"];
        selectedLanguage1=[dummy objectAtIndex:0];
        NSLog(@"english bookshelf button:%@",selectedLanguage1);
        homeController.selectedLanguage = selectedLanguage1;  
        NSLog(@"sel lang:%@",homeController.selectedLanguage);

     } else if (indexPath.row ==1) {
        //bookshelf button
        NSMutableDictionary *french=[[NSMutableDictionary alloc]init];
        french=[allDictElements objectForKey:@"French Language"];
        dummy=[french objectForKey:@"French"];
        selectedLanguage1=[dummy objectAtIndex:0];
        NSLog(@"french bookshelf button:%@",selectedLanguage1);
        homeController.selectedLanguage = selectedLanguage1;
        NSLog(@"sel lang:%@",homeController.selectedLanguage);

    } else {

        NSMutableDictionary *spanish=[[NSMutableDictionary alloc]init];
        spanish=[allDictElements objectForKey:@"Spanish Language"];
        dummy=[spanish objectForKey:@"Spanish"];
        selectedLanguage1=[dummy objectAtIndex:0];
        NSLog(@"spanish bookshelf button:%@",selectedLanguage1);
        homeController.selectedLanguage = selectedLanguage1;
        NSLog(@"sel lang:%@",homeController.selectedLanguage);
    }

    [self.navigationController pushViewController:homeController animated:YES];
}

在这个控制器中,我很好地接收到选定的语言字符串。

//LeeValleyHomeViewController.h

NSString *selectedLanguage;

@property (nonatomic,retain) NSString *selectedLanguage;

 //LeeValleyHomeViewController.m

@synthesize selectedLanguage;

- (void)viewDidLoad
{
    LanguageViewController lvc = [LanguageViewController alloc]init];
    bookshelfbtn.titleLabel.text = lvc.selectedLanguage1;
    NSLog(@"button title:%@",bookshelfbtn.titleLabel.text);
   //bookshelfbtn.titleLabel.text = selectedLanguage;
}

在此控制器中,没有收到该字符串值。我的代码有什么问题?或者我该怎么做?提前致谢。

4

2 回答 2

1

您可以将所选语言保存在 NSUserDefaults 中,然后在 LeeValleyHomeViewController.m 的 ViewDidLoad 中检索它,如下所示.....

在你的,

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

    // 选择语言后

    [[NSUserDefaults standardUserDefaults]setValue:selectedLanguage1 forKey:@"language"]; [[NSUserDefaults standardUserDefaults]synchronize];

    }

//LeeValleyHomeViewController.m

  • (void)viewDidLoad

{

NSLog(@"button title:%@",bookshelfbtn.titleLabel.text);
bookshelfbtn.titleLabel.text =  [[NSUserDefaults standardUserDefaults]valueForKey forKey:@"language"];

}
于 2012-12-05T06:08:00.360 回答
0

我不确定您在第一个代码块中的“”是否与您在第二个代码块中的LanguageController“”相同LanguageViewController

但如果是的话,你为什么认为selectedLanguage1在你实例化那个“”之后“”属性会被设置为零以外的任何值LanguageViewController?用户尚未触摸任何单元格,因此它可能是唯一的语言是英语(值为零)。

于 2012-12-05T06:04:14.843 回答