In iPhone and iPad, you must be familiar with one thing: settings.
To put it bluntly, I learned how to set certain attributes of an app in settings. In turn, some attribute values changed in the app will also be reflected in settings, this function is very common and simple to implement, but there are still many contents.
First of all, let's make a simple description of settings. Although we often enable settings, we seldom perform a careful study on settings. However, as an iOS developer, it is necessary to explore settings to see what is included in the settings and what these are.
First, open settings to see the main interface of settings (the screen is captured from the iPhone simulator)
As you can see, the main interface of settings is a table view, and the attribute of style is grouped. In addition to common general, airplane mode, Wi-Fi, and notifications (the last three are not displayed in the middle. Open your iPhone and these options will be available in settings ), the iPhone creates a cell for each desired app, such as Twitter and Facebook. Click Twitter to view some settings contained in the app.
The setting items in settings are restricted. They can contain the following four types: text field, switch, Slider, and tableview checklist.
As you can see, settings is actually a navigation controller, and the relationship between views is inherited.
In iOS, how does one operate settings? This is called nsuserdefaults. It can easily interact with settings in the app and save the updated value.
Now, let's start with this example. After this example is completed, we believe we will have a better understanding of settings.
1) create a project, select Application on the left, utility application on the right, and click Next.
In the product name field, enter the required parameter, select use storyboards, and click Next.
Click create.
Observe the project file automatically created by the program, which contains a common bidappdelegate, and the use storyboard we just selected, so there is a mainstoryboard. storyboard exists. In addition to these, there are also four files, namely, bidmainviewcontroller and bidflipsideviewcontroller. This is automatically created by the utility application template.
This is the first time that we use Utility application. So here we will give a brief introduction. Utility application will automatically create two views for us. The first view is called main view, the second view is flipside view. There is an information button on the main view. Clicking this information button will switch to flipside view. There is a done button on the navigator bar of the flipside view. Click the done button to switch back to the main view. Select mainstoryboard. storyboard in project navigator and you will see the view 2, which corresponds to the bidmainviewcontroller and bidflipsideviewcontroller respectively.
Now, we need to add a settings bundle. If an app needs to be set in settings, you must add settings bundle in your project, when you install your app on the iPhone, it automatically adds an entry related to the project (cell) in settings for setting.
2) Add settings bundle
Select File> New> file..., select resource on the left, set settings bundle on the right, and click Next.
Retain the default name and click "CREATE" to add it.
Expand settings. bundle, which has two default items. One is en. lproj folder (this is temporarily ignored. You do not need to pay attention to it now), and the other is root. plist, we can conclude from the suffix that the items in settings are based on the property list, that is, an XML file.
Select Property list. The following content is displayed in editor Pane:
Let's change the display mode of root. List. Right-click any part of editor pane and select "show raw keys/Values" in the pop-up menu"
Then Root. plist will display the following information:
The most intuitive discovery is that the names of the two keys have changed, "Preference items" has changed to "preferencespecifiers", and "strings FILENAME" has changed to "stringstable". In fact, the content has not changed, the display method is different. Let's look at my personal preferences. The former name is more popular, and the latter name is closer to the implementation.
In addition, we do not need to pay too much attention to stringstable. This value is used for localization, so you can delete it directly here.
3) add controls in settings
Add text field
Then we expand perferencespecifiers, which contains four default items, which are generated by the system for us, but these are not what we want in this example, we delete item 1, Item 2, and item 3, and only retain item 0.
(I also deleted stringstable)
Next, expand Item 0 to see what is in it.
There is a title and type key-Value Pair (that is, dictionary). We focus our attention on type. Its value is psgroupspecifier, which indicates that this item is a group type, all item of the same level after this item belongs to this group until the next group type appears. In preferencespecifiers, at least one group must exist. Title is used to set the group name. This attribute can be omitted.
Observe the display method of item 0: Item 0 (group-group). In the brackets, the first group is only the type of the item psgroupspecifier, the second group is the title value of this item. Modify the title value to "General Info"
After setting, you will find that the display of item 0 is changed to: Item 0 (group-General info)
Psgroupspecifier is not a type that can be actually operated. Its function is to include several related attributes to form a group in Table view. Next we will add operable nodes.
First, combine Item 0, then select the entire line of item 0, and click return on the keyboard. A new item (item 1) appears below item 0, A list is displayed, allowing you to select the type of this item.
Here we select text field. After item 1 is created, expand item 1.
We can see that there are three key-value pairs in total. The type is pstextfieldspecifier, the other two are title and key, and the title is assigned "username ", assign the key to "username" (case sensitive ). The key here is used to save and obtain the value of Item 1. Each item can be understood as a dictionary, and a dictionary is a key-value pair, the key is used to obtain or save the specified value. (There is no key in item 0, because Item 0 is of the type psgroupspecifier and there is no value to save)
At the beginning of this article, we mentioned nsuserdefaultes. nsuserdefaultes uses this key to obtain the item value and stores the updated value.
Finally, we add two more properties for item 1, select the last row of item 1 (the row where the key is located), and press return on the keyboard, A new row appears under the key. There is a list for you to select the key name. Here we select "autocapitalizationtype" and set the value to "NONE ". Add the same method again. Select "autocorrectiontype" this time, and set the value to "no"
"Autocapitalizationtype" indicates whether the input is automatically completed. That is to say, if you enter the first few words of a word, the system will display a list with related words in the list. You only need to select the words directly, the input can be completed. Here we set it to "NONE", without automatic completion.
"Autocorrectiontype" means to automatically correct the spelling, that is, whether the system helps you build the correct spelling for the words you enter. We also turn off this function.
Okay. Save root. plist.
So far, we can try to compile and run the program. To enable the program to be highlighted in settings (find it easy), we add an icon for the program and download an icon first, in the project navigator, select the root node appsettings, select appsetetunder targets on the left, open the summary tab, expand the iPhone/iPod deployment info, and find app icons.
Drag the icon to the icon on the left (the icon for the Retina screen is displayed on the right. We didn't provide this icon here, so leave it empty)
All right, compile and run the program. After the program starts, press the Home Key to return to the desktop, and enter settings. At the bottom of the main interface of settings, find the appsettings program icon we created.
Click this icon to enter the settings of this program.
We can see in the root. in plist, the result of restoring the items we set is a group, the group title is General info, and then a textfield item in group, and its title is username.
OK. At this point, you should have a certain understanding of settings, know how the items are generated, and the role of each item attribute. Next we will add more different types of items to perform comprehensive settings operations.
3) add other items
Add secure text field
First, close item 1, select it, press command + C, command + V on the keyboard, copy and paste an item, a new item 2 will appear under item 1
Expand item 2 and set its attributes to the following
As shown in, item 2 is used to receive a password. Its type is pstextfieldspecifier. set its title to password and key to password, here there is a new attribute called issecure. When the value is set to yes, the content in the text box will be displayed as a password. (You can compile and run it to see if the password box works)
Add multivalue Field
Multivalue field generates a cell with an arrow. Clicking this row will jump to the next table view. The next table view contains multiple options, select one of the multiple options and return to the previous view. The content in the cell is the content selected by the user.
We combine item 2, select item 2, press return to create item 3, and select the type of item 3 as multi value.
Expand Item 3. You can see that the type is psmultivaluespecifier. Set the title to protocol and the key to protocol.
OK. Now we will add a set of titles and values, titles to save the display values of each option, and values to save the IDs of each option, which correspond one to one. Retain the expanded form of item 3, select item 3, and press the return key. (In the expanded form of item, select and press the retuan key to add the subitem of the current item; in the form of item closure, press the return key to add the same level item). A new item of item 3 is created and the type of the selected item appears in the drop-down box, here we select titles.
Repeat the preceding action, create a subitem of item 3, and select values.
Select titles, click the plus sign, add a subitem, and assign a value to HTTP
Repeat this operation to add the following values: SMTP, nntp, IMAP, POP3
Use the same method to add an item for values. After adding the item, it will look as follows:
Values and titles are different in that one is lowercase and the other is uppercase. Of course, if you use uppercase or lowercase, it is okay.
Compile and run a program
Enter the password in passwrod and a small dot is displayed. An arrow is added to the right of the Protocol item. Click this item to jump to the next view.
Here is the added titles content.
Select the status after SMTP, and click the reset ettings button in the upper left corner to return to the previous view.
The selected SMTP is displayed on the right of the Protocol entry.
Add toggle switch settings
Toggle switch is a simple switch, which can be enabled or disabled. The purpose of this type is to set a bool value.
Combine item 3, select, and press return to add an item 4, set the type of item 4 to toggle switch, expand item 4, and set title to "warp drive ", the key is "warp", and the defaultvalue value is changed to Yes
Okay. Compile and run the command. A switch appears under protocol.
Add slider setting
Slider is well known. In settings, a slider can place an image on each side of the iPhone (but I found that there are not many examples of placing images on the iPhone ), slider itself does not contain text instructions, and we cannot place a label in settings to tell users what the role of this slider is. Therefore, the solution here is to add a new group, then, add a text description for the group to inform the user of the role of the slider.
Combine item 4, select, press return, add item 5, select the type of item 5 as group, and set the title value as "warp factor"
Combine item 5, select, and press return to add item 6. Select the slider type of item 6 and set the attributes of item 6.
Compile and run the program. The effect is as follows:
As we said just now, you can add an image (the image size is 21*21 pixel) at both ends of the slider. Let's add it now. First download the image here.
There are some special ways to add an image to a slider. We didn't drag the image directly to the project navigator and put it in the slider. settings didn't provide us with this method. We can add them using other methods. First, right-click Settings. bundle in project navigator and select Show in finder.
Right-click Settings. bundle in the finder and select Show package contents)
Copy the two images into the package, and then you can see the two images in the project navigator.
Next, open root. plist, add two items max value image filename and min value image filename to item 6, assign rabbit to Max, and assign turtle to Min.
Compile and run the program again. The two images are displayed on the left and right sides of the slider.
Add a child settings View
This means to jump to another settings view by clicking a cell on table view. After some of the above explanations, we are actually operating on plist. It can be inferred that all settings are based on one plist file, so if you want to jump to another settings view, that must include another plist. Based on this idea, we will perform the following operations.
Create another group, create it under item 6, and name it title.
Then we add item 8 under item 7. According to the previous method, now we should set the type of item 8, however, the default drop-down box does not contain the required type (Child pane). If you are not in a hurry, expand Item 8 (here make sure you have selected show raw keys/values ), click the button at the end of the value column of its type row to display a drop-down box.
Select pschildpanespecifier in the drop-down box, so that item 8 is a child pane.
The title is set to "more settings", and the key is empty. Because this is a navigation item, we don't need to get its value, so we don't need its key.
Next, select the last key, press return to add one, and select file from the drop-down menu.
We need to associate a plist file so that we can navigate to another settings view and download more. plist. Do you still remember how we added two images to the slider just now? Right-click Settings in project navigator. bundle, select Show in finder, and right-click setting in the finder. bundle, select Show package contents, set more. plist is copied to the project navigator so that more is displayed in the project navigator. plist.
Because more. plist is ready to help us, because we don't need to perform any operations on it, we can use it directly. Compile and run the program
The more settings we just added are added at the bottom of the settings view. Click More settings to jump to the settings view of more. plist.
We can see that all the first four in more are text fields, and the last is mulitvalue field. You can just click here. That's all about it.
4) Summary
So far, all the controls that can be added in settings have been introduced. There are not many general classes and there are 6 in total. The settings operation is the operation on a plist file, the IOS system automatically reflects the content in plist to settings. You only need to perform operations on plist to complete the settings of the settings view. In the next article, we will connect the value in settings with the real app. After setting the value in settings, it will be reflected in the app and a value will be changed in the app, the value in settings will also be updated. The bridge connecting these two things is the nsuserdefaults mentioned earlier. We will provide a detailed introduction in the next article. Thank you!
Appsettings