在我的应用程序的开始屏幕中,您可以通过单击英国或瑞典的旗帜来选择英语和瑞典语之间的语言。 问题是当您单击按钮时,ViewDidLoad无法识别NSUserDefaults中的更改。但是当您重新启动应用程序时,语言是您单击的最新标志!因此它保存了NSUserDefault,但它只在ViewDidLoad中第一次加载..
单击英文标记时,sprakval设置为0,如果单击瑞典标志,则sprakval设置为1.当您单击标记时,它将更改为标记前面带有刻度图标的图像。 / p>
代码:
-(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.
}
答案 0 :(得分:1)
viewDidLoad是从nib文件加载视图后调用的方法。你不应该手动调用它。
如果您已编写代码来刷新viewDidLoad中的控件,请将其移入另一个方法并从按钮事件处理程序中调用该方法。
- (void)adjustControlsForLanguage
{
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];
}
}
viewDidLoad中
- (void)viewDidLoad{
[super viewDidLoad];
[self adjustControlsForLanguage];
}
按钮事件处理程序
-(IBAction) sprakEN {
sprakval=0;
NSUserDefaults *sprakvalet = [NSUserDefaults standardUserDefaults];
[sprakvalet setInteger:sprakval forKey:@"Sprak "];
[sprakvalet synchronize];
[self adjustControlsForLanguage];
}
-(IBAction) sprakSE {
sprakval=1;
NSUserDefaults *sprakvalet = [NSUserDefaults standardUserDefaults];
[sprakvalet setInteger:sprakval forKey:@"Sprak "];
[sprakvalet synchronize];
[self adjustControlsForLanguage];
}
编辑:由于您使用的是基于tabBar的应用程序,最好使用viewWillAppear重新加载特定于语言的控件
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self adjustControlsForLanguage];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
答案 1 :(得分:1)
这是因为您通过-(IBAction) sprakSE
和-(IBAction) sprakEN
方法中的超类调用ViewDidload方法。因此,请在两种方法中将
[super viewDidLoad];
替换为
[self viewDidLoad];
。它会正常工作。
希望它对你有所帮助。