ios - 应用程序进入/离开后台时保存和加载数据

标签 ios iphone objective-c appdelegate nskeyedarchiver

我试图让我的游戏保持其当前状态并在用户退出或重新启动游戏时重新加载它。然而,我正在努力解决的问题是如何在正确的时间实际访问这些对象。这是我需要的伪代码,我似乎无法访问正确的对象,我是在倒退还是这是正确的方法?

重申一下,我的问题是访问正确的 ViewControllers 以便从磁盘保存/加载数据。

我的导航层次结构很简单,ViewController > GameViewController(模态显示)

-(void)applicationDidEnterBackground:(UIApplication *)application
{
  // Save current state to disk

  // See if GameViewController (or GameView) is top controller (aka game in progress)
  // If so then use NSKeyedArchiver to persist to disk
}

-(void)applicationDidBecomeActive:(UIApplication *)application
{
  // Load current state from disk

  // Use NSKeyedArchiver to load data from disk, if game is in progress then
  // Find mainViewController, then show the game modal on top of it
  // then populate the game data with the data from disk
}

额外的问题:这两个函数是我保存/加载的正确函数吗?

最佳答案

或者,您可以在代码中任何您想要/需要的地方设置一个通知处理程序:

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnteredBackground:) 
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];

关于ios - 应用程序进入/离开后台时保存和加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20400712/

相关文章:

iphone - 核心数据、iCloud 和 App 被锁定的系统文件暂停

iOS:RestKit 操作阻止在我想要的时候执行

iphone - Monotouch.Dialog:启用 ScopeBar 时禁用自动搜索

ios - 此处 map 中未获得当前位置

iOS – 收到 UILocalNotification 后呈现 MFMessageComposeViewController

iphone - 仅在 iphone 上测试通用应用程序

ios - 在QML程序中在iOS上播放音频文件

iphone - iOS——从包含地址的 NSString 获取指针

ios - 在 UIDatePicker 中选择特定日期?

objective-c - 静态库体积大