0

By the suggestion of SO folk in a previous question (here), I'm in the process of making my primary model object a singleton to share between a "main" view controller and a "settings" view controller. I think this setup will be great once it's complete, but I'm having an issue with laying it out in an MVC-friendly manner.

At the moment, my settings view currently initializes settings on viewDidLoad if none exist (via NSUserDefaults), lays out subviews depending on what buttons are selected (settings view holds all settings button outlets), updates a dictionary of button states when a button is pressed, saves those updated to the NSUserDefaults, updates the settings panel subviews, and sends the button settings to the singleton.

Meanwhile, my singleton converts the button settings to a dictionary of settings that the model object will understand & updates the model object with the information created so the main view controller can update its collection view with the new model objects.

I'm thinking laying out the subviews should be handled by the settings view. My question is am I having the settings view do too much of the problem? If so, should I have my singleton/model object update button states (therefore receiving an array of UIButtons from the view), and handle any NSUserDefaults-related tasks?

4

1 回答 1

1

也许只有我一个人,但这一切听起来都不必要地复杂

如果您有一个要处理设置信息的模型,为什么您的视图直接处理 NSUserDefaults?模型的工作是管理信息。如果您的设置信息的“模型”本质上就像一个字典,即您有一堆键/值对,那么 NSUserDefaults 可能就是您需要的所有模型,至少对于设置而言。或者,如果您在创建包含设置信息的模型时遇到了麻烦,您应该依赖模型来存储和检索信息;不要背着你的模型直接访问信息。

因此,您可以执行以下操作:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL foo = [defaults boolForKey:@"fooSetting"];

您需要访问“foo”设置的任何地方,或者您可以在模型中执行一次,并让应用程序中的其他所有内容在需要该设置时访问模型。任何一个都可以,但不要两个都做。

另外:我不在乎回答您其他问题的人说什么,您不需要将模型设为单例,这样您就可以从多个视图控制器访问它。我不会在这里讨论整个单例好与单例坏的论点,但请确保您谷歌一下,至少了解这个问题。

于 2013-07-01T04:52:57.603 回答