3

I have an application that is based on a UINavigationController; I wish to add a "Setting" page where the user will have the ability to set some features like Language and some other preferences. Currently the UIViewController where I wish to have the Setting fields in is 2 levels under the RootViewController (i.e. there is a "main view" >> you click on a button and enter another UIViewController and form there you should be able to enter the Setting UIViewController).

I'm not clear about how I'm supposed to save this data and how to call it upon application load.

I read some blogs about NSUserDefaults and about Singleton but I'm not clear how should I use them.

Where should I create the data attributs that will later on maintain the user preferences? Should I create them on the AppDelegate or on the MySettingsViewController (the UIViewController that I'm creating)?

Should I use a Singleton attribute, and if so, where should it be created?

When you say "Singleton", do you actually mean creating a Static attribute?

Is there another way to communicate between 2 controllers that are not directly connected one to the other (I can transfer data from the "bottom" ViewController to the RootViewController passing it via the UIViewController in the middle, but it seems weird and ineffective)?

Any direction / tutorial will be appreciated!

Ohad Regev
  • 5,641
  • 12
  • 60
  • 84

2 Answers2

12

Definitely use NSUserDefaults. It's great, and Apple recommends it.

To set a setting:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Setting 1"];

You can also store other things, such as text, numbers, etc. Much more than a simple boolean.

To check the setting:

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Setting 1"]) {
    //ok, do the thing here
}
Jack Humphries
  • 13,056
  • 14
  • 84
  • 125
  • InAppSettingsKit uses NSDefaults behind the scenes. Why reinvent the wheel? – coneybeare Aug 01 '11 at 17:32
  • I didn't see the other comment, but instead of download the kit, it's just two lines if you want to do one thing. And NSUserDefaults can be used for many more things than just settings. For example, remembering an in app purchase. – Jack Humphries Aug 01 '11 at 20:47
  • Jack, how exactly am I supposed to use what you had suggested? I have several questions about it: – Ohad Regev Aug 02 '11 at 10:59
  • 1. do I need to "#import" any headers for this? – Ohad Regev Aug 02 '11 at 11:00
  • 2. If I "save" the user preference in UIViewController number 3 in the stack, can I then simply call it from the 1st UIViewController or do I need to pass the data in some way (like have a singleton object or pass it via the popViewController method back through the stacked UIViewControllers)? – Ohad Regev Aug 02 '11 at 11:02
  • OK, I gave it a try and figured out how to save and retrieve the data. Thanks for that!!! Now, I'm not clear about how to refresh the data in other UIViewControllers that are supposed to be affected. can you take a look at another question that I had posted? http://stackoverflow.com/questions/6912706/ios-how-to-refresh-a-uiviewcontroller-after-popping-its-child-uiviewcontroller – Ohad Regev Aug 02 '11 at 13:40
  • [userDefauls synchronize]; – melanke Dec 11 '13 at 12:37
4

I'd use http://inappsettingskit.com/ rather than roll my own. I've used it in almost every application I work on and it handles app settings perfectly for just this sort of scenario.

Roger
  • 15,793
  • 4
  • 51
  • 73