在 iPhone MVC 应用程序中处理用户首选项的最佳实践? [英] Best practices for handling user preferences in an iPhone MVC app?

查看:29
本文介绍了在 iPhone MVC 应用程序中处理用户首选项的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个 iPhone 应用程序,在实现用户偏好时,我有一些关于如何最好地实现它的问题.

I'm developing my first iPhone app, and while implementing user preferences, I have a few questions on how to best implement this.

假设基本的MVC模式:MainView显示存储在Model对象中的数据;MainViewController 处理应用程序逻辑.

Assume the basic MVC pattern: MainView displaying data stored in a Model object; MainViewController handling the app logic.

对于选项,我有一个 OptionsView 和一个 OptionsViewController.RootViewController 处理 Main 和 Options 视图的交换.

For the options, I have an OptionsView and an OptionsViewController. A RootViewController handles swapping of the Main and Options views.

首先,选项数据呢?我是否想要创建一个 NSOjbect 派生类来存储它们(OptionsModel)?或者一个简单的 NSDictionary 就足够了?

First of all, what about the options data? Would I want to make an NSOjbect-derived class to store them (an OptionsModel)? Or maybe a simple NSDictionary would suffice?

第二,谁拥有期权数据?它们是应用程序首选项,但将它们存储在 AppDelegate 中似乎是错误的.MainViewController 主要关心选项,OptionsViewController 需要编辑它们,但这些家伙彼此不了解,也不应该知道.RootViewController 知道两者,所以他可以在它们之间传递选项,但同样,这似乎是错误的.

Second, who owns the options data? They are application prefs, but storing them in the AppDelegate seems wrong. MainViewController mostly cares about the options, and OptionsViewController needs to edit them, but those guys don't know about each other, nor should they, probably. RootViewController knows about both, so he's in a good position to pass the options between them, but again, that seems wrong.

最后,MainViewController 将经常需要访问选项.他应该每次调用选项模型来获取一些选项,还是缓存他自己的副本?我不喜欢检索选项数据的额外开销的想法,但是让 MainViewController 具有可能不同步的副本也不是很吸引人.

Finally, MainViewController will frequently need to access the options. Should he call into the options model each time to get some option, or cache his own copy? I don't like the idea of extra overhead retrieving options data, but having the MainViewController have copies which can get out of sync is also not very appealing.

推荐答案

看看 NSUserDefaults.它是专为此特定目的而设计的字典样式集合类.

Have a look at NSUserDefaults. It is a dictionary style collection class designed for this particular purpose.

您可以像这样存储和检索默认值:

You store and retrieve defaults like so:

// Fetch a default
BOOL deleteBackup = [[NSUserDefaults standardUserDefaults] boolForKey:@"DeleteBackup"];

// Save a default
[[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"DeleteBackup"];

NSUserDefaults 将自动负责读取和序列化到磁盘.

NSUserDefaults will automatically take care of reading from and serializing to disk.

关于您的另一个问题,这取决于您应用的整体架构.在 OptionsViewController 完成时发送通知对我来说是一种合理的方法.但是,您也可以在 MainViewController 或应用程序委托(即-reloadUserDefaults")上定义一个方法,您可以在它完成后从 OptionsViewController 调用该方法.

Concerning your other question, it depends on the overall architecture of your app. Sending out notifications when the OptionsViewController is finished sounds like a reasonable approach to me. However, you could also just define a method on your MainViewController or app delegate (ie. "-reloadUserDefaults") that you can invoke from your OptionsViewController when it's finished.

这篇关于在 iPhone MVC 应用程序中处理用户首选项的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆