在我的应用程序的开始屏幕中,您可以通过单击英国或瑞典的国旗在英语和瑞典语之间选择语言。问题是当您单击按钮时 ViewDidLoad 无法识别 NSUserDefaults 中的更改。但是当您重新启动应用程序时,该语言是您单击的最新标志!所以它保存了 NSUserDefault 但它只在 ViewDidLoad 中第一次加载它。
当你点击英国国旗时,sprakval 设置为 0,如果你点击瑞典国旗,sprakval 设置为 1。当你点击一个旗帜时,它会变成一个在旗帜前面带有勾号图标的图像。
代码:
-(IBAction) sprakEN
{
sprakval=0;
NSUserDefaults *sprakvalet = [NSUserDefaults standardUserDefaults];
[sprakvalet setInteger:sprakval forKey:@"Sprak "];
[sprakvalet synchronize];
[super viewDidLoad];
}
-(IBAction) sprakSE
{
sprakval=1;
NSUserDefaults *sprakvalet = [NSUserDefaults standardUserDefaults];
[sprakvalet setInteger:sprakval forKey:@"Sprak "];
[sprakvalet synchronize];
[super viewDidLoad];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *sprakvalet = [NSUserDefaults standardUserDefaults];
sprakval2 = [sprakvalet integerForKey:@"Sprak "];
if (sprakval2==0)
{
spraklabel.text = [NSString stringWithFormat:@"Language:"];
[lhb setTitle:@"english" forState:UIControlStateNormal];
[hlb setTitle:@"english" forState:UIControlStateNormal];
[fhb setTitle:@"english." forState:UIControlStateNormal];
[blandatb setTitle:@"english.." forState:UIControlStateNormal];
UIImage *encheck = [UIImage imageNamed:@"United_Kingdomchecked.png"];
[enbutton setImage:encheck forState:UIControlStateNormal];
UIImage *seuncheck = [UIImage imageNamed:@"Sweden.png"];
[sebutton setImage:seuncheck forState:UIControlStateNormal];
self.title = @"Game";
}
else if(sprakval2==1)
{
spraklabel.text = [NSString stringWithFormat:@"Språk:"];
[lhb setTitle:@"swedish" forState:UIControlStateNormal];
[hlb setTitle:@"swedish" forState:UIControlStateNormal];
[flb setTitle:@"swedish" forState:UIControlStateNormal];
[fhb setTitle:@"swedish" forState:UIControlStateNormal];
[blandatb setTitle:@"swedish" forState:UIControlStateNormal];
self.title = @"Spel";
UIImage *secheck = [UIImage imageNamed:@"Swedenchecked.png"];
[sebutton setImage:secheck forState:UIControlStateNormal];
UIImage *enuncheck = [UIImage imageNamed:@"United_Kingdom.png"];
[enbutton setImage:enuncheck forState:UIControlStateNormal];
}
// Do any additional setup after loading the view, typically from a nib.
}