iOS: How to Create a "User Preference" Feature
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 UIView开发者_JAVA百科Controller 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!
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
}
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.
精彩评论