在iPhone MVC应用程序中处理用户首选项的最佳实践?

时间:2022-07-03 20:04:39

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

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

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

假设基本MVC模式:MainView显示存储在模型对象中的数据;MainViewController处理app逻辑。

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

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

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?

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

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.

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

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.

最后,MainViewController经常需要访问选项。他应该每次都调用options模型来获取某个选项,还是缓存自己的副本?我不喜欢额外的开销获取选项数据的想法,但是让MainViewController有可以脱离同步的副本也不是很有吸引力。

1 个解决方案

#1


8  

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

看看NSUserDefaults吧。它是为这个特殊目的而设计的字典样式集合类。

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 will automatically take care of reading from and serializing to disk.

NSUserDefaults将自动处理从磁盘的读取和序列化。

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.

关于你的另一个问题,它取决于你的应用的整体架构。在OptionsViewController完成后发送通知听起来对我来说是一个合理的方法。但是,你也可以在你的MainViewController或app委托上定义一个方法。“-reloadUserDefaults”)当它完成时,您可以从OptionsViewController中调用它。

#1


8  

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

看看NSUserDefaults吧。它是为这个特殊目的而设计的字典样式集合类。

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 will automatically take care of reading from and serializing to disk.

NSUserDefaults将自动处理从磁盘的读取和序列化。

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.

关于你的另一个问题,它取决于你的应用的整体架构。在OptionsViewController完成后发送通知听起来对我来说是一个合理的方法。但是,你也可以在你的MainViewController或app委托上定义一个方法。“-reloadUserDefaults”)当它完成时,您可以从OptionsViewController中调用它。