Application Settings Bundle Part – II

If you haven’t checked my previous post, I would recommend you to read it first. So, just go through following.

The user can get to our preferences, but how do we get to them? We’ll take advantage of a class, NSUserDefaults, to read in the user’s settings. NSUserDefaults is implemented as a singleton, which means there is only one instance of NSUserDefaults running in our application. To get access to that one instance, we call the class method standardUserDefaults:

We use it just like an NSDictionary once we have a pointer to the standard user defaults. To get a value out of it, we can call objectForKey: which will return an Objective_C object like an NSString, NSDate, or NSNumber. If we want to retrieve the value as a scalar like an int, float, or BOOL, we can use other methods, such as intForKey:, floatForKey:, or boolForKey:.

When we were creating the property list for this application, we created an array of PreferenceSpecifiers. Some of those specifiers were use to create groups. Others created interface objects that the user used to set their settings. Those are the specifiers we are really interested in, because that’s where the real data is. Every specifier that was tied to a user setting had a Key named Key. For instance, the Key for our slider had a value of warpfactor. The Key for our Password field was password. We’re going to use those keys to retrieve the user settings.

Now that we have a place to display the settings, let’s set up our main view with a bunch of labels. Before going over to Interface Builder, we need to create outlets for all the labels. Let’s make some changes to the interface file, “MainViewController.h”

 

 

We declared several constants. These are the key values that we used in our property list file for the different preference fields. Then, we declared ten outlets, all of them labels, and we created properties for each of them. After that, we declared a method that will read settings out of the user defaults and push those values into the various labels.

Open Interface Builder by double-clicking MainView.xib in Resources folder.

Let’s change the dark gray view to white by using attribute inspector. Use the color well labeled Background to change the background to white.

 

 

Now, it’s time to add 20 labels to the Main View. Half of them will be static labels that are right-aligned and bold: the other half will be used to display the actual values retrieved from the user defaults and will have outlets pointing to them. Make them as in the picture below.

 

 

The implementation file, “MainViewController.m” is:

 

 

Now build and Run the Application.

 

Changing the Values into NSDefaults

 

Our main view is now up and running. Let’s build the flipside view. The flipside view features our warp drive switch, as well as the warp factor slider. We’re going to use the same controls that the Settings application uses for these two items: a switch and a slider. First, we should declare our outlets. Here is the interface file, “FlipsideViewController.h”:

 

 

Back to Interface Builder by double-clicking the FlipsideView.xib. Change the background color using the attribute inspector to a lighter shade of gray, about 25% gray. We are going to design the flipside view as the picture below.

Here is the implementation file, “FlipsideViewController.m”:

 

Now Reset your simulator and build then Run the application shows like below screen.

 

Hence Download the Source code From Here.

Thanks And Regards,
Nimit Parekh

Leave a Reply

Your email address will not be published. Required fields are marked *



*