使用 NSUserDefaults 存储 UISwitch 状态

发布于 2024-10-03 10:45:55 字数 2382 浏览 0 评论 0原文

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

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:

-(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 中,我编写了这行代码:

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

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

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

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

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

syncSwitch.on = syncStartupSwitch;

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

[[NSUserDefaults standardUserDefaults]synchronize];

不过,在模拟器或设备上重新启动应用程序后,开关状态不会恢复...

什么上面的代码是我的错误吗?

干杯,

iSee

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

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

    }
}

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?

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

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

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;

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?

Cheers,

iSee

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心病无药医 2024-10-10 10:45:55

您可以使用以下代码;


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

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


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

只需测试上面的代码即可;

you can use following code;


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

if you use following code, it will return YES;


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

Just test above code;

微凉 2024-10-10 10:45:55

不要使用 setObject:forKey: 尝试使用 setBool:forKey:。

[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"SyncAtStartup"];

Instead of using setObject:forKey: try using setBool:forKey:.

[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"SyncAtStartup"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文