在我的應用程序的開始屏幕中,您可以通過單擊英國或瑞典的標誌來選擇英語和瑞典語之間的語言。 問題是,當您單擊按鈕時,ViewDidLoad無法識別NSUserDefaults中的更改。但是,當您重新啓動應用程序時,語言是您點擊的最新標誌!因此它保存了NSUserDefault,但它只在ViewDidLoad中第一次加載它。從ViewDidLoad中的NSUserDefaults加載數據
當您單擊英語國旗時,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.
}
'@「Sprak」'是有目的的空間嗎? – 2013-04-21 17:11:11
嗯,沒有。但我確定這不是問題! – Mangy92 2013-04-21 17:12:41
檢查我的答案。它對你有用嗎? – 2013-04-21 18:15:08