iPhone sdk如何接收字符串值?

时间:2012-12-05 05:50:56

标签: iphone string plist

在我的应用程序中,我需要提供三种语言支持。我的默认语言是英语。一旦用户在设置控制器中选择其他语言,整个应用程序应该更改为该特定语言。为此,我决定使用包含所有三种语言及其数据的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;
}

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

2 个答案:

答案 0 :(得分:1)

您可以在NSUserDefaults中保存所选语言,并在LeeValleyHomeViewController.m的ViewDidLoad中检索它,就像这样......

在你的

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

    {

    //选择语言后

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

    }

// LeeValleyHomeViewController.m

  • (无效)viewDidLoad中

{

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

}

答案 1 :(得分:0)

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

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