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