0

我正在尝试保存 UISwitch 的状态。如果 UISwitch 状态为“ON”并且当用户退出应用程序并再次启动它时..应用程序应该显示以前的状态以及用户是否计划将 UISwitch 的状态更改为“OFF” ..他应该收到一条消息,说他之前处于“ON”状态并更改为“OFF”状态

如果你们帮助我,那就太好了。谢谢

-(IBAction)notification:(id)sender
{
    if (sender == notifyMe)
    {
        if(notifyMe.isOn == YES)
        {
            toggle = YES;
            NSUserDefaults* defaults  = [NSUserDefaults standardUserDefaults];
            [defaults setBool:notifyMe.on forKey:@"switchValueKey"];
            [defaults synchronize];
            NSLog(@"Notification is ON");
        }
        else 
        {
            toggle = NO;
            NSLog(@"Notification is OFF");
        }
    }
    if ([cellDelegate respondsToSelector:@selector(notificationReqd:)])
    {
        [cellDelegate notificationReqd:self];
    }
}
4

1 回答 1

1

更改您的按钮操作方法,例如:

-(IBAction)notification:(id)sender
{
    if (sender == notifyMe)
    {
         NSUserDefaults* defaults  = [NSUserDefaults standardUserDefaults];
         BOOL previousState = [defaults boolForKey:@"switchValueKey"];
        if(notifyMe.isOn == YES)
        {
            toggle = YES;

            NSLog(@"Notification is ON");
        }
        else 
        {
            toggle = NO;
            NSLog(@"Notification is OFF");
        }
        [defaults setBool:toggle forKey:@"switchValueKey"];
        [defaults synchronize];

         //You can show the message here
         NSLog(@"Previous state was %d",previousState);
    }
    if ([cellDelegate respondsToSelector:@selector(notificationReqd:)])
    {
        [cellDelegate notificationReqd:self];
    }
}

当您想获取存储的数据时,您可以使用:

 NSUserDefaults* defaults  = [NSUserDefaults standardUserDefaults];
 BOOL previousState = [defaults boolForKey:@"switchValueKey"];
于 2012-12-10T11:10:46.250 回答