保存 UISwitch 结果和 NSUserDefaults

发布于 2025-01-03 01:50:18 字数 2249 浏览 1 评论 0原文

几天来我一直在尝试保存 tableViewController 行上的 UISwitch 状态,但无济于事。我查看了各种 stackOverflow 问题和回复以及苹果文档,但似乎在我的应用程序中没有任何效果。

这是我的应用程序委托:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"No" forKey:@"emailSwitch"];

    [defaults registerDefaults:appDefaults];

    [defaults synchronize];
}

我在 Settings.bundle 集中有 Root.plist,这似乎有效并将切换结果保存在应用程序设置中,但不在 tableViewController 上。例如,如果我在设置中将其切换为“打开”,则当我将对象添加到我的 tableViewController 时,开关会变为“关闭”。

以下是 tableViewController.h 文件中的相关代码:

@property (nonatomic, getter=isOn) BOOL on;

- (void)toggleValue:(id)sender;

以下是 tableViewController m 文件中的相关代码:

- (void)viewWillAppear:(BOOL)animated
{    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"emailSwitch"]) 
    {
        [emailSwitch setOn:YES animated:NO];
    }
    else
    {
        [emailSwitch setOn:NO animated:NO];
    } 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    customTableViewCell *cell = (customTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

//Code regarding cell contents here

    UISwitch *switchObj = [[UISwitch alloc] initWithFrame:CGRectZero];

    [switchObj addTarget:self action:@selector(toggleValue:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];

    cell.accessoryView = switchObj; 

    return cell;
}

-(void)toggleValue:(id)sender
{
    [[NSUserDefaults standardUserDefaults] setBool:emailSwitch.on forKey:@"emailSwitch"];

    [[NSUserDefaults standardUserDefaults] synchronize];
}

当您将对象添加到 tableViewController 并将 UISwitch 切换为“on”时,不保存“on”状态。我尝试了各种方法,但没有任何效果。如果有人能告诉我我做错了什么,我将非常感激。预先感谢您可以提供的任何帮助。

I have been trying for days to save the UISwitch state on my tableViewController row to no avail. I have looked at the various stackOverflow questions and responses and the apple documentation, but nothing seems to work in my app.

Here is my app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"No" forKey:@"emailSwitch"];

    [defaults registerDefaults:appDefaults];

    [defaults synchronize];
}

I have the Root.plist in the Settings.bundle set, and this seems to be working and saving the switch results in the app settings but not on the tableViewController. For example, if I switch it to "on" in settings, when I add an object to my tableViewController the switch comes up "off".

Here is the pertinent code in the tableViewController.h file:

@property (nonatomic, getter=isOn) BOOL on;

- (void)toggleValue:(id)sender;

Here is the pertinent code in the tableViewController m file:

- (void)viewWillAppear:(BOOL)animated
{    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"emailSwitch"]) 
    {
        [emailSwitch setOn:YES animated:NO];
    }
    else
    {
        [emailSwitch setOn:NO animated:NO];
    } 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    customTableViewCell *cell = (customTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

//Code regarding cell contents here

    UISwitch *switchObj = [[UISwitch alloc] initWithFrame:CGRectZero];

    [switchObj addTarget:self action:@selector(toggleValue:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];

    cell.accessoryView = switchObj; 

    return cell;
}

-(void)toggleValue:(id)sender
{
    [[NSUserDefaults standardUserDefaults] setBool:emailSwitch.on forKey:@"emailSwitch"];

    [[NSUserDefaults standardUserDefaults] synchronize];
}

When you add an object to the tableViewController and switch the UISwitch to "on", it does not save the "on" status. I have tried various approaches, but nothing is working. If anyone could tell me what I'm doing wrong, I would really appreciate it. Thanks in advance for any help you can offer.

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

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

发布评论

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

评论(1

一口甜 2025-01-10 01:50:18

如果我正确理解你的问题,你的toggleValue方法应该如下所示:

-(void)toggleValue:(UISwitch*)sw
{
  [[NSUserDefaults standardUserDefaults] setBool:sw.on forKey:@"emailSwitch"];

  [[NSUserDefaults standardUserDefaults] synchronize];
}

If I understand your question correctly, your toggleValue method should look like this:

-(void)toggleValue:(UISwitch*)sw
{
  [[NSUserDefaults standardUserDefaults] setBool:sw.on forKey:@"emailSwitch"];

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