【问题标题】:How to store settings on iPhone immediately?如何立即在 iPhone 上存储设置?
【发布时间】:2012-02-20 12:40:33
【问题描述】:

是否可以立即在 iPhone 上存储设置? iOS 4+ 上的应用程序在退出时不会关闭,如果我在 Xcode 设置中按“停止进程”,则不会保存?如何保存?

- (void)compLoad {
    compTotal = [[NSUserDefaults standardUserDefaults] integerForKey: @"compTotal"];
    compCount = [[NSUserDefaults standardUserDefaults] integerForKey: @"compCount"];
    compShow = [[NSUserDefaults standardUserDefaults] boolForKey: @"compShow"];
}

- (void)compSave {
    [[NSUserDefaults standardUserDefaults] setInteger: compTotal forKey: @"compTotal"];
    [[NSUserDefaults standardUserDefaults] setInteger: compCount forKey: @"compCount"];
    [[NSUserDefaults standardUserDefaults] setBool: compShow forKey: @"compShow"];
}

【问题讨论】:

    标签: iphone xcode ipad ios4 nsuserdefaults


    【解决方案1】:

    [[NSUserDefaults standardUserDefaults] 同步];

    同步 - 此方法会定期自动调用 间隔,只有当你不能等待自动 同步(例如,如果您的应用程序即将退出)或 如果您想将用户默认更新为磁盘上的内容,即使 您没有进行任何更改。

    【讨论】:

      【解决方案2】:

      根据文档,以下调用将保留所有未完成的修改。要检查这是否成功,请检查此调用的返回值。 YES 值表示成功。

      [[NSUserDefaults standardUserDefaults] synchronize];
      

      【讨论】:

      • 你能提供更多关于它是如何失败的信息吗?您设置和获取用户默认值的代码可能会有所帮助?
      【解决方案3】:

      在您的AppDelegate 中致电您的compSave

      - (void)applicationWillResignActive:(UIApplication *)application 
      {
          // Call your compSave here to force saving before app is going to the background
      }
      

      还有

      - (void)compSave 
      {
          [[NSUserDefaults standardUserDefaults] setInteger: compTotal forKey: @"compTotal"];
          [[NSUserDefaults standardUserDefaults] setInteger: compCount forKey: @"compCount"];
          [[NSUserDefaults standardUserDefaults] setBool: compShow forKey: @"compShow"];
          [[NSUserDefaults standardUserDefaults] synchronize];
      }
      

      【讨论】:

      • 你在我上面提到的AppDelegate方法中调用了你的compSave吗?
      • 每次变量改变后我都会调用它。
      【解决方案4】:

      只需致电[[NSUserDefaults standardUserDefaults] synchronize];。但通常你确实不需要:一旦你的应用程序进入后台,iOS 就会保存,在其他一些情况下也会保存。过于频繁地调用synchronize 被认为是一件坏事(tm)。

      【讨论】:

      • 数据是否重要——这是件好事)感谢您的回答。
      • 因为synchronize 是正确的解决方案(我确定,过去几年我在多个项目中使用过 NSUserDefaults)你的问题一定出在其他地方。也许您在某处覆盖了您的设置?例如,如果您在应用程序启动时不小心在compLoad 之前调用compSave,您将再次获得“空”值。在compSavecompLoad 上设置断点,然后查看启动应用时首先调用哪个断点。
      【解决方案5】:

      尝试使用synchronize 方法。

      更新:您应该考虑注册您的默认设置,就像它在Preferences and Settings Programming Guide 中所建议的那样:

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
         // Register the preference defaults early.
         NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"CacheDataAgressively"];
         [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
      
         // Other initialization...
      }
      

      此外,从本指南(“同步和检测首选项更改”)中,关于同步:

      为了检测偏好值何时发生更改,应用还可以注册通知 NSUserDefaultsDidChangeNotification。共享的 NSUserDefaults 对象在检测到位于其中一个持久域中的首选项发生更改时,会将此通知发送到您的应用程序。您可以使用此通知来响应可能影响您的用户界面的更改。例如,您可以使用它来检测用户首选语言的更改并适当地更新您的应用内容。

      如果您想直接重新读取设置,请务必使用synchronize

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-19
        • 1970-01-01
        • 2011-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多