NSuserdefaults UISwitch

时间:2014-06-11 14:37:49

标签: label nsuserdefaults uiswitch

我在保存开关位置方面遇到一些问题,有什么帮助?

- (IBAction)SWitchActionAño:(id)sender {
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
if (switchAño.on) {
     [standardDefaults setBool:YES forKey:@"SwitchState"];
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"MMM dd,yyyy HH:mm"];
    diadelasemanalabel.text=[formatter stringFromDate:[NSDate date]];
} else {
    [standardDefaults setBool:NO forKey:@"SwitchState"];
    Año.text=@"";
}

}

但我有问题!我不知道这是什么属性!

- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
//the error here:property switchAño not found on object....
if ([standardDefaults objectForKey:@"SwitchState"])
    self.switchAño.on =[standardDefaults boolForKey:@"SwitchState"];

1 个答案:

答案 0 :(得分:0)

在viewDidLoad中尝试此操作

-(void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *standardDefaults = [[NSUserDefaults standardUserDefaults];
if ([standardDefaults boolForKey:@"SwitchState"] == YES) {
    self.switchAño.on = YES;
}
else {
    self.switchAño.on = NO
}