私は5つのスイッチを持っています。位置は を使用して保存され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];
}