Reputation: 157
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];
}
Upvotes: 0
Views: 115
Reputation: 135
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
Upvotes: 3
Reputation: 1029
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.
Upvotes: 0