【发布时间】:2013-12-05 13:00:20
【问题描述】:
我正在尝试让我的游戏保持其当前状态并在用户退出或重新启动游戏时重新加载它。然而,我正在努力解决的是如何在正确的时间实际访问对象。这是我需要的伪代码,我似乎无法访问正确的对象,我是在倒退还是这样做是正确的方法?
所以重申一下,我的问题是访问正确的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
}
额外问题:这两个函数是我保存/加载的正确函数吗?
【问题讨论】:
-
为什么不让每个 viewController 成为应用程序委托中的一个属性?
-
我将如何实现这一目标?
-
在您的应用程序委托的头文件中(上述方法所在的位置)声明@property UIViewController *yourController;那么您可以根据需要访问您的 VC,并且您始终有参考...
-
谢谢,但我如何将属性绑定到我的 ViewControllers?顺便说一句,我正在使用界面生成器
-
将 NSMutableDictionary 设置为应用委托中的属性。在某个特定时间(如阶段完成)访问您的 VC 中的此字典变量。这里将您的游戏状态存储在字典中。然后在 applicationWillEnterBackground 中将您的字典数据写入本地数据中的 prefrences/plist。并在 applicationWillEnterForeground 从本地目录中读取并存储到您的字典变量中。
标签: ios iphone objective-c appdelegate nskeyedarchiver