应用程序关闭时如何保存值

发布于 2024-09-08 08:00:38 字数 191 浏览 10 评论 0原文

我是 iPhone 编程新手..

我的应用程序就像一个测验..它有不同类型的测验,每种测验类型都有很多问题 我想在应用程序关闭时保存字符串(测验名称)和整数(问题编号)等值 因此,当应用程序重新启动时,我想通过使用保存的值继续停止的位置

如何执行此操作...?

任何人都可以帮我做到这一点吗...

谢谢你

i am new to iPhone programming..

my app is just like a quiz.. it has different type of quizs and many question in each quiz type
i want to save the values like strings(quiz name) and integers(question number) when an application is closed
so when the app is restarted i want to continue where it was stopped by using saved values

How to do this...?

Can any body help me to do this please....

Thank u

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

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

发布评论

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

评论(3

梦冥 2024-09-15 08:00:38

保存字符串:

NSString *name = @"John";
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:name forKey:@"Name"];

加载字符串:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *name = [prefs stringForKey:@"Name"];

阅读更多信息:

To save a string:

NSString *name = @"John";
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:name forKey:@"Name"];

To load a string:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *name = [prefs stringForKey:@"Name"];

Read more at: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

如果没有 2024-09-15 08:00:38

我建议您查看核心数据并在输入时保存答案。然后当应用程序重新启动时,你需要找到一种方法来快速加载相关数据(实际上,你会发现你不需要加载那么多,这也可以帮助你减少需要保存的数据。)

记住,简单这是本质,延迟加载非常有帮助。在需要之前不要加载任何不需要的东西。

Apple 的一个很好的例子是他们随 iPhoneOS 2.0 提供的 SQLiteBooks 示例(我不知道他们是否仍然提供)。

I would suggest you look at Core data and save answers as they are entered. Then when the app is restarted you need to find a way to quickly load the relevant data (Actually, you will find you don't need to load that much, which could also help you reduce what you need to save.)

Remember, simplicity is of the essence, and lazy loading very helpful. Don't load anything you don't need until you need it.

A good Apple example was the SQLiteBooks sample which they provided with iPhoneOS 2.0 (I don't know if they still provide it).

书信已泛黄 2024-09-15 08:00:38

NSUserDefaults 是有利于保存状态和首选项。

NSUserDefaults is good for saving state and preferences.

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