Question

I basically have two switch buttons that i want the user to select from either two player or group play. However i dont want the user to be able to select both of these so idealy when the user clicks one the other turns off. How would be best to implement this?

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


        [defaults synchronize];
    }

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

        [defaults synchronize];
    }
Was it helpful?

Solution

Do you have references for both switches ? If yes, it will be something like this :

-(void)stateSwitchedtwoplayer:(id)sender {
    UISwitch *tswitch = (UISwitch *)sender;
    self.switchGroup.on =! tswitch.isOn; //reference to group switch
    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;
    self.switchTwoPlayer.on =! tswitch.isOn; //reference to two players switch
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    [defaults setBool: tswitch.isOn forKey:@"groupswitch"];
    [defaults setBool: tswitch.isOn forKey:@"twoplayerswitch"];
    [defaults synchronize];
}

but if you want that both switches can be off, then you just need to change it on

self.switchGroup.on =! tswitch.isOn == YES; //reference to group switch
self.switchTwoPlayer.on =! tswitch.isOn == YES; //reference to two players switch

OTHER TIPS

You would implement this with KVO. You can find documentation at: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueObserving/Articles/KVOCompliance.html. But i would suggest you not to.

You should implement some UISegmentedControl for these type of operations. Why don't you take a look at: https://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top