在我的应用程序的开始屏幕中,您可以通过单击英国或瑞典的国旗在英语和瑞典语之间选择语言。
问题是,单击按钮时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.
}
最佳答案
这是因为您正在通过-(IBAction) sprakSE
和-(IBAction) sprakEN
方法中的超类调用ViewDidload方法。因此,在两种方法中都将 [super viewDidLoad];
替换为 [self viewDidLoad];
。它会正常工作。
希望对您有帮助。