关闭app后保存开关位置

时间:2017-02-21 20:51:32

标签: ios objective-c nsuserdefaults uiswitch

我想知道如何保存开关的位置以关闭和打开应用程序?

-(IBAction)switch_poids_passagers:(UISwitch *)sender {

 NSString *poids_passagers_total;
 if(sender.on) {
      poids_passagers_total = @"1";

 } else {
      poids_passagers_total = @"2";

 }

 [[NSUserDefaults standardUserDefaults] setObject:poids_passagers_total forKey:@"poids_passagers_total"];
 [[NSUserDefaults standardUserDefaults] synchronize];

}

1 个答案:

答案 0 :(得分:0)

NSUserDefaults可以使用它提供的配对方法保存和检索BOOLS

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:self.myUISwitch.on forKey:@"switchValue"];
// ...

稍后,也许在viewWillAppear ......

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL switchValue = [defaults boolForKey:@"switchValue"];
self.myUISwitch.on = switchValue;