关闭应用程序后保存开关位置

Save switch position after closing app

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

-(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];

}

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;