In my application, i need to provide three languages support. My default language will be English. Once the user selects other language in setting controller the entire application should change into that particular language. For this i decided to have a plist file with all the three languages and their data in it.Now my default language is English that is fine. Now i changed the language using plist in LanguageController and pushed to home controller. The selected language(French) string was not received in my home controller. Here is my code,
//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];
}
Here in this controller am receiving the selected language string finely.
//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;
}
Here in this controller am not receiving that string value. What’s wrong with my code? or how can i do this? Thanks in advance.
I’m not certain if “
LanguageController” you have in the first block of code is the same thing as “LanguageViewController” in your second block of code.But if it is, why do you think the “
selectedLanguage1” property would be set to anything other than zero after you instantiate that “LanguageViewController“? The user hasn’t touched any cell yet so the only language that it could possibly be is English (with a value of value zero).