为什么我的uiswitch会感到困惑

时间:2014-03-26 23:46:23

标签: ios objective-c uiswitch

我在一个视图控制器上有4个ui开关,两个工作正常,但最后两个已设置好,所以你只能点击任意一组或两个播放器,但当你点击两个播放器然后点击保存并返回其中打开两个开关然而如果你在组中做同样的事情,它不能这样做吗?

任何人都能看到我在这里做错了什么?

   -(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];

1 个答案:

答案 0 :(得分:1)

stateSwitchedtwoplayer方法中,您可能会更改self.groupswitch.on的状态,但您不会将更改保存到NSUserDefaults。