1

我有五个开关。使用 保存位置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];



}
4

3 回答 3

1

UIControlEventValueChanged当您在代码中设置值时,不会调用事件的目标和选择器。此外,您没有保存其他开关的状态,因此您将在On位置保存多个开关。最后,您可以合并操作方法,因为您已经在进行sender识别。

于 2013-08-06T17:54:30.777 回答
0

如果您有 5 个开关,但在任何给定时间只能打开一个,而所有其他开关将始终关闭,为什么只有一个可以做到时有 5 个变量?在 NSUserDefaults 中存储一个 int 或 w/e。制作视图时,检查存储的 int 是什么,并将除适当的开关之外的所有开关设置为关闭。切换开关时,更改存储的值并以编程方式切换所有其他开关。如果可以将所有开关设置为关闭(没有打开),则 0 或 -1 值适用于这种情况。

正如其他答案中所指出的,当您以编程方式设置值时,不会调用 UIControlEventValueChanged 。如果您坚持为您的开关设置 5 个不同的变量,那么当任何一个被切换时,您必须以编程方式切换所有其他变量(您已经这样做了)并设置所有其他变量的布尔值。

无论如何,UI 元素应该始终为其状态引用一个自变量,而不是自己控制其状态。不管你怎么做,改变它都会解决这个问题。

于 2013-08-06T19:05:00.630 回答
0

您应该遵循 MVC 范式。您不应将状态存储在 UI 控件中。相反,设置 UI 控件以匹配您的模型/数据的状态,并让您的 UI 控件更改您的模型/数据的状态。

实现此目的的一种简单方法是为每个具有当前状态(是或否)的开关设置一个布尔变量或属性。

这些变量/属性值可以很容易地从NSUserDefaults.

在您的viewWillAppear中,您根据变量设置 UI 控件的值。

然后,当用户更改开关的值时,在方法中设置变量/属性的值IBAction

现在,您的初始视图正确显示。即使值从不同的视图控制器更改,您的开关仍将显示正确的值。

于 2013-08-06T18:46:13.257 回答