There are a number of applications in iOS settings related to the settings, as shown in the following figure:
This setting makes it easy to make changes to some of the basic settings that are applied.
To fully implement this setup function, there are several issues to be resolved:
1 The writing of the setting (the function of implementing settings)
2 Set the storage of content (ensure that the settings are changed to take effect immediately.) For example, minimize the application, and then change in the settings, reopen the application, should show the new settings.
You can refer to the Apple Development documentation for this content: Preferences and Settings programming Guide
Let's say the implementation process:
Step 1: Write Settings
Implemented by the settings bundle.
1 Create a new settings Bundle:
Once you've created it, you can see the following:
As we can see, the settings bundle contains two files, one is root.plist used to set the content to set, and the other root.strings is used for multiple languages. We do not consider the content of root.plist now, just look at the setting of the method.
We see that there are several items in the Root.plist file that you want to set up. A total of 7 types:
These are well understood, except that the child pane are some controls, but this is done through the plist file. Child pane is a sub page, which can be a bit more cumbersome. Let's talk about the front ones first.
First look at the effect you want:
1) Group
The settings are as follows:
2) Text Field
The settings are as follows:
Here is the identifier storage need to use the key, is to use nsuserdefaults, then introduced.
Then the text Field is secure this option is to set whether to encrypt. Everything else is easy to understand.
3) Toggle Switch
4) Slider
Can add left and right pictures
5) Multi Value
The settings are as follows:
Here is a description of the use of child pane.
To add a new settings page, you need to create a new plist.
The diagram above is an excerpt from the development document and can be clearly seen in the structure.
So we need to create a new plist file.
The key point is that we can't create in the Xcode because we can't pull the file into the bundle in Xcode, so the easiest way is to open the bundle File Manager and copy root.plist and rename ...
The next step is to set up child Pane:
FileName is the name of the new plist without a suffix.
And then in Child.plist's editor and Root.plist. The key point is that for easy editing, you should set the Child.plist type to the iphone settings plist
OK, through the above method, we can complete the desired setup function on the interface. The following is the operation of the data.
Step 2: Use Nsuserdefaults to implement preference storage.
First of all, it is clear that the above setting data will be stored in the Nsuserdefaults standarddefaults, each setting corresponding to the identifier is key.
1 generally we need to set some initial data, that is, the first time the application to open the data can be the initial default settings.
Code:
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
//Override point for customization after application launch.
Nsdictionary *defaults = @{kscreenautolock: @NO,
ksensitivityvalue: @5};
[[Nsuserdefaults Standarduserdefaults] registerdefaults:defaults];
Return YES
}
Set through a dictionary. The kscreenautolock here is the macro definition key, which is identifier
2 in obtaining data
Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];
Value = [Defaults floatforkey:ksensitivityvalue];
Switch = [defaults boolforkey:kscreenautolock];
3) Change the data
Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];
[Defaults setfloat:5.0f forkey:ksensitivityvalue];
[Defaults synchronize];
Remember to use synchronize to implement data storage.
4) Supplementary
Using the above method alone is not enough, the problem is if we make a change in the settings, then this is if the application is started, open the application after the data because there is no synchronization, or the data before setting. Therefore, when the program is Enterforeground, the application should synchronize the data.
In use, it is through notification to synchronize the changed data at enterforeground time
Code:
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (applicationwillenterforeground:) Name: Uiapplicationwillenterforegroundnotification Object:nil];
-(void) Applicationwillenterforeground: (nsnotification *) notification
{[
nsuserdefaults Standarduserdefaults] synchronize];
[Self refreshsettings]; Update settings Display
}
The content about the settings is introduced here.