简体   繁体   English

如何在plist文件上保存UISwitch状态?

[英]How to save UISwitch state on plist file?

i am trying to save the state of UISwitch .If the UISwitch state is "ON" and when the user quits the app and starts it again ..the app should show the previous state and if the user plans to change the state of UISwitch to "OFF" ..he should get a message saying previously he had "ON" state and change to state to "OFF" 我正在尝试保存UISwitch的状态。如果UISwitch的状态为“ ON”,并且当用户退出应用程序并再次启动它时..应用程序应显示先前的状态,并且如果用户计划将UISwitch的状态更改为“ OFF” ..他应该收到一条消息,说他之前处于“ ON”状态,然后将状态更改为“ OFF”

if you guys help me out that would be great .Thanks 如果你们帮我的忙,那太好了。谢谢

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

Change your button action method like: 更改您的按钮操作方法,例如:

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

When you want to get the stored data you can use: 当您想要获取存储的数据时,可以使用:

 NSUserDefaults* defaults  = [NSUserDefaults standardUserDefaults];
 BOOL previousState = [defaults boolForKey:@"switchValueKey"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM