我有五个开关。使用 保存位置NSSUserDeafults
。
如果 aUISwitch
被切换.on
,其他人会转到关闭位置。所有开关均尊重用户选择位置并正确保存。
问题是UISwitch
位置只会在手动切换时保存,而不是在.on = NO
发件人调用时保存。on
这意味着如果在加载视图时的任何时间点都已将所有开关设置为,则它们将全部.on
设置为,直到另一个开关被关闭并再次打开。
开关移至关闭位置,但不保存(除非手动完成)
- (IBAction)plainToggled:(id)sender {
if (sender == _plainToggle) {
_curvesToggle.on = NO;
_cloudsToggle.on = NO;
_circlesToggle.on = NO;
_fadeToggle.on = NO;
BOOL mySwitchValuePlain = [ sender isOn ];
NSString *tmpStringPlain = mySwitchValuePlain ? @"1" : @"-1" ;
NSUserDefaults *myNSUDPlain = [NSUserDefaults standardUserDefaults];
[myNSUDPlain setBool:TRUE forKey:@"switchBOOLPlain"];
[ myNSUDPlain setObject:tmpStringPlain forKey: @"mySwitchValueKeyPlain" ];
[ myNSUDPlain synchronize ];
//save a key to read switch state in another view
NSUserDefaults *userDefaultsPlain = [NSUserDefaults standardUserDefaults];
[userDefaultsPlain setBool:self.plainToggle.on forKey:@"switchPlain"];
[userDefaultsPlain synchronize];
}
}
- (IBAction)curvesToggled:(id)sender {
if (sender == _curvesToggle) {
_cloudsToggle.on = NO;
_circlesToggle.on = NO;
_fadeToggle.on = NO;
_plainToggle.on = NO;
BOOL mySwitchValueCurves = [ sender isOn ];
NSString *tmpStringCurves = mySwitchValueCurves ? @"1" : @"-1" ;
NSUserDefaults *myNSUDCurves = [NSUserDefaults standardUserDefaults];
[myNSUDCurves setBool:TRUE forKey:@"switchBOOLCurves"];
[ myNSUDCurves setObject:tmpStringCurves forKey: @"mySwitchValueKeyCurves" ];
[ myNSUDCurves synchronize ];
//save a key to read switch state in another view
NSUserDefaults *userDefaultsCurves = [NSUserDefaults standardUserDefaults];
[userDefaultsCurves setBool:self.curvesToggle.on forKey:@"switchCurves"];
[userDefaultsCurves synchronize];
}
}
- (IBAction)cloudsToggled:(id)sender {
if (sender == _cloudsToggle) {
_curvesToggle.on = NO;
_circlesToggle.on = NO;
_fadeToggle.on = NO;
_plainToggle.on = NO;
BOOL mySwitchValueClouds = [ sender isOn ];
NSString *tmpStringClouds = mySwitchValueClouds ? @"1" : @"-1" ;
NSUserDefaults *myNSUDClouds = [NSUserDefaults standardUserDefaults];
[myNSUDClouds setBool:TRUE forKey:@"switchBOOLClouds"];
[ myNSUDClouds setObject:tmpStringClouds forKey: @"mySwitchValueKeyClouds" ];
[ myNSUDClouds synchronize ];
//save a key to read switch state in another view
NSUserDefaults *userDefaultsClouds = [NSUserDefaults standardUserDefaults];
[userDefaultsClouds setBool:self.cloudsToggle.on forKey:@"switchClouds"];
[userDefaultsClouds synchronize];
}
}
- (IBAction)circlesToggled:(id)sender {
if (sender == _circlesToggle) {
_curvesToggle.on = NO;
_cloudsToggle.on = NO;
_fadeToggle.on = NO;
_plainToggle.on = NO;
BOOL mySwitchValueCircles = [ sender isOn ];
NSString *tmpStringCircles = mySwitchValueCircles ? @"1" : @"-1" ;
NSUserDefaults *myNSUDCircles = [NSUserDefaults standardUserDefaults];
[myNSUDCircles setBool:TRUE forKey:@"switchBOOLCircles"];
[ myNSUDCircles setObject:tmpStringCircles forKey: @"mySwitchValueKeyCircles" ];
[ myNSUDCircles synchronize ];
//save a key to read switch state in another view
NSUserDefaults *userDefaultsCircles = [NSUserDefaults standardUserDefaults];
[userDefaultsCircles setBool:self.circlesToggle.on forKey:@"switchCircles"];
[userDefaultsCircles synchronize];
}
}
- (IBAction)fadeToggled:(id)sender {
if (sender == _fadeToggle) {
_curvesToggle.on = NO;
_cloudsToggle.on = NO;
_circlesToggle.on = NO;
_plainToggle.on = NO;
BOOL mySwitchValueFade = [ sender isOn ];
NSString *tmpStringFade = mySwitchValueFade ? @"1" : @"-1" ;
NSUserDefaults *myNSUDFade = [NSUserDefaults standardUserDefaults];
[myNSUDFade setBool:TRUE forKey:@"switchBOOLFade"];
[ myNSUDFade setObject:tmpStringFade forKey: @"mySwitchValueKeyFade" ];
[ myNSUDFade synchronize ];
//save a key to read switch state in another view
NSUserDefaults *userDefaultsFade = [NSUserDefaults standardUserDefaults];
[userDefaultsFade setBool:self.fadeToggle.on forKey:@"switchFade"];
[userDefaultsFade synchronize];
}
}
读取开关状态viewDidLoad
//switch value for plain cert
NSUserDefaults *myNSUDPlain = [NSUserDefaults standardUserDefaults];
NSString *tmpStringPlain = [ myNSUDPlain stringForKey: @"switchPlain"];
BOOL mySwitchValuePlain = YES; // or DEFAULT_VALUE
if (tmpStringPlain != nil) {
mySwitchValuePlain = ( [ tmpStringPlain intValue ] == 1 );
}
[_plainToggle setOn: mySwitchValuePlain];
//switch value for curves cert
NSUserDefaults *myNSUDCurves = [NSUserDefaults standardUserDefaults];
NSString *tmpStringCurves = [ myNSUDCurves stringForKey: @"switchCurves"];
BOOL mySwitchValueCurves = NO; // or DEFAULT_VALUE
if (tmpStringCurves != nil) {
mySwitchValueCurves = ( [ tmpStringCurves intValue ] == 1 );
}
[_curvesToggle setOn: mySwitchValueCurves];
//switch value for clouds cert
NSUserDefaults *myNSUDClouds = [NSUserDefaults standardUserDefaults];
NSString *tmpStringClouds = [ myNSUDClouds stringForKey: @"switchClouds"];
BOOL mySwitchValueClouds = NO; // or DEFAULT_VALUE
if (tmpStringClouds != nil) {
mySwitchValueClouds = ( [ tmpStringClouds intValue ] == 1 );
}
[_cloudsToggle setOn: mySwitchValueClouds];
//switch value for circles cert
NSUserDefaults *myNSUDCircles = [NSUserDefaults standardUserDefaults];
NSString *tmpStringCircles = [ myNSUDCircles stringForKey: @"switchCircles"];
BOOL mySwitchValueCircles = NO; // or DEFAULT_VALUE
if (tmpStringCircles != nil) {
mySwitchValueCircles = ( [ tmpStringCircles intValue ] == 1 );
}
[_circlesToggle setOn: mySwitchValueCircles];
//switch value for fade cert
NSUserDefaults *myNSUDFade = [NSUserDefaults standardUserDefaults];
NSString *tmpStringFade = [ myNSUDFade stringForKey: @"switchFade"];
BOOL mySwitchValueFade = NO; // or DEFAULT_VALUE
if (tmpStringFade != nil) {
mySwitchValueFade = ( [ tmpStringFade intValue ] == 1 );
}
[_fadeToggle setOn: mySwitchValueFade];
}