2014-03-26 74 views
0

我有一個視圖控制器和兩個4個UI開關的工作完美但近兩年已經設置這樣你就可以在任何一組或然兩個播放,當你在兩個玩家點擊,然後單擊單擊僅保存並返回它將兩個開關打開,但是如果你在組中做同樣的事情,dosnt不會這樣做?爲什麼我uiswitch的越來越糊塗

任何人看到什麼即時做錯了什麼?

-(void)stateSwitched:(id)sender { 
    UISwitch *tswitch = (UISwitch *)sender; 
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; 
    [defaults setObject: tswitch.isOn ? @"YES" : @"NO" forKey:@"truthonoff"]; 

    [defaults synchronize]; 
} 

-(void)stateSwitcheddare:(id)sender { 
    UISwitch *tswitch = (UISwitch *)sender; 
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; 
    [defaults setBool: tswitch.isOn forKey:@"groupswitch"]; 
    [defaults setBool: tswitch.isOn forKey:@"twoplayerswitch"]; 
    [defaults synchronize]; 
} 

-(void)stateSwitchedtwoplayer:(id)sender { 
    UISwitch *tswitch = (UISwitch *)sender; 
    //turns two player off when on and soforth 

    self.groupswitch.on =! tswitch.isOn; 
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; 
    [defaults setBool: tswitch.isOn forKey:@"twoplayerswitch"]; 
    [defaults setBool: !tswitch.isOn forKey:@"groupswitch"]; 

    [defaults synchronize]; 
} 

-(void)stateSwitchedgroup:(id)sender { 
    UISwitch *tswitch = (UISwitch *)sender; 
    //turns two player off when on and soforth 
    self.twoplayerswitch.on =! tswitch.isOn; 
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; 
    [defaults setBool: tswitch.isOn forKey:@"groupswitch"]; 
    [defaults setBool: tswitch.isOn forKey:@"twoplayerswitch"]; 
    [defaults synchronize]; 
} 

剛剛意識到即時通訊運行在viewdidload這可能會影響它呢?

[self.twoplayerswitch setOn:[[defaults objectForKey:@"twoplayerswitch"] boolValue] animated:YES]; 

[self.twoplayerswitch addTarget:self action:@selector(stateSwitchedtwoplayer:) forControlEvents:UIControlEventValueChanged]; 

[self.groupswitch setOn:[[defaults objectForKey:@"groupswitch"] boolValue] animated:YES]; 

[self.groupswitch addTarget:self action:@selector(stateSwitchedgroup:) forControlEvents:UIControlEventValueChanged]; 
+0

看看我以前的答案,我忘了先改變默認值。如果你改變例如twoplayerswitch,你也需要更新默認值 – cwir

+0

對不起,我現在困惑了,你能解釋一下嗎? – user3246508

+0

我改爲下面的代碼,但仍然得到錯誤 – user3246508

回答

1

stateSwitchedtwoplayer方法,你可能改變國家的self.groupswitch.on,但你不更改保存到NSUserDefaults的。

相關問題