Save and Load UISwitch state

[亡魂溺海] 提交于 2019-12-03 05:06:46

Did you create the switch as an IBOutlet with properties? If so, I think your problem is that you dont refer to your switchControl as self.switchControl.

Then your correct saving statement will become

[[NSUserDefaults standardUserDefaults] setBool:self.switchControl.on forKey:@"switch"];
[[NSUserDefaults standardUserDefaults] synchronize];

I'd move the part that sets the switch into viewWillAppear and do it like this:

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  BOOL test= [[NSUserDefaults standardUserDefaults] boolForKey:@"switch"];
  NSLog(@"%@",test?@"YES":@"NO");
  [self.switchControl setOn:test animated:YES];
}

I took out an unnecessary if-statement for you, too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!