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

查看:109
本文介绍了在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处理app逻辑。

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天全站免登陆