使用NSUserDefaults存储UISwitch状态 [英] Using NSUserDefaults for storing UISwitch state

查看:149
本文介绍了使用NSUserDefaults存储UISwitch状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序的设置视图中保留 UISwitch 状态。基本上它是一个 UITableView ,并包含一些开关来获取用户首选项。下面的代码解释了如何构造开关(下面只给出了一个开关结构,其他也是相同的构造)。

I am trying to persist the UISwitch state in my settings view of my application. Basically it is a UITableView and contains a few switches to get the user preferences. The below code explains how the switches are constructed (only one switch construct is given below, others are also constructed the sameway).

if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SomeIdentifierB] autorelease];
            if (syncStartupSwitch) {
                syncSwitch.on = YES;
            }else {
                syncSwitch.on = NO;
            }

            [syncSwitch addTarget:self action:@selector(syncAtStartup:) forControlEvents:UIControlEventValueChanged];


            NSLog(@"Why is this not working%@",(syncSwitch.on ? @"YES" : @"NO"));
                [cell.contentView addSubview:syncSwitch];
            cell.accessoryView = syncSwitch;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
                //cell.reuseIdentifier  = @"Cell1";
        }
        cell.textLabel.text =cellValue;
        return cell;
    }

现在,我想使用NSUserDefaults存储交换机的状态。所以在我的选择器方法实现中,我定义了NSUserDefaults,如下所示:

Now, I would like to store the state of the Switches using NSUserDefaults. So in my selector method implementation, I defined the NSUserDefaults like this:

-(void) syncAtStartup:(id)sender {
    if ([sender isOn]) {
        [[NSUserDefaults standardUserDefaults]
         setObject:@"YES" forKey:@"SyncAtStartup"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        NSLog(@"%@",(syncStartupSwitch ? @"YES" : @"NO"));

    }else {
        [[NSUserDefaults standardUserDefaults]
         setObject:@"NO" forKey:@"SyncAtStartup"];
            //syncStartupSwitch = [[NSUserDefaults standardUserDefaults]boolForKey:@"SyncAtStartup"];

    }
}

最后,在我的viewDidLoad中写道这行代码:

Finally, in my viewDidLoad I wrote this line of code:

syncStartupSwitch = [[NSUserDefaults standardUserDefaults]boolForKey:@"SyncAtStartup"];

我确信我的实现缺少一些逻辑。有人可以指出这个缺陷并纠正我吗?

I am sure there is some missing logic to my implementation. Can somebody point out the flaw and correct me?

更新:
我接受了@jfalexvijay的建议并使用了以下代码:

UPDATE: I took the suggestion from @jfalexvijay and used the below code:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"SyncAtStartup"];
BOOL syncStartupSwitch = [[NSUserDefaults standardUserDefaults] boolForKey:@"SyncAtStartup"];

当我查看Preferences文件夹时,我看到plist是用BOOL值创建的。然后我尝试在cellForRowAtIndexPath方法中设置UISwitch状态,如下所示:

When I look into the Preferences folder, I see the plist getting created with the BOOL value in it. I then try to set the UISwitch state in cellForRowAtIndexPath method like this:

syncSwitch.on = syncStartupSwitch;

我在ApplicationWillTerminate和选择器本身也有这行代码

I also have this line of code in ApplicationWillTerminate and in the selector itself

[[NSUserDefaults standardUserDefaults]synchronize];

但是,在模拟器或设备上重启应用程序后,交换机状态仍未恢复...

Still, after restarting the application on the simulator or device, the switch state is not restored...

上述代码中的错误是什么?

What is my mistake in the above code?

干杯,

iSee

推荐答案

您可以使用以下代码;

you can use following code;


[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"SyncAtStartup"];
BOOL test = [[NSUserDefaults standardUserDefaults] boolForKey:@"SyncAtStartup"];

如果您使用以下代码,它将返回YES;

if you use following code, it will return YES;


[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"SyncAtStartup"];
BOOL test = [[NSUserDefaults standardUserDefaults] boolForKey:@"SyncAtStartup"];

只需测试以上代码;

这篇关于使用NSUserDefaults存储UISwitch状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆