Starting from scratch iOS development (19): Application Settings and User Defaults (top)

Source: Internet
Author: User

On the iphone and ipad, there's one thing that everyone must be familiar with, and that thing is settings.

The thing to learn plainly is simple, is to learn how to set some properties of an app in Settings, in turn, in the app to change some property values, will also be reflected in the settings, this function is very common, the implementation is relatively simple, but the content is more.

First of all, a simple explanation of the settings, although we often open settings, but very few settings have been carefully studied, but as an iOS developer, it is necessary to explore settings, Look at what's inside settings and what it is.

First we open the settings, we can see the main interface of settings (the screen is intercepted from the iphone simulator)

As you can see, the main interface of settings is a table View,style property is grouped. In addition to the common general, airplane Mode, Wi-Fi, notifications, etc. (3 not appearing in the following, open your iphone,settings will have these options), The iphone creates a cell for every app that needs it, such as Twitter, Facebook, and more. Click Twitter to see some of the settings that the app contains.

The settings in Settings are limited and can contain the following 4 categories: Text Field,switch,slider, TableView checklist.

As you should see, settings is actually an inheritance relationship between navigation controller,view and view.

In the way iOS is about how to operate settings, this thing is called nsuserdefaults, it can be conveniently in the app and settings to interact with, save updated values.

Well, here's a brief introduction, so let's start with this example, and I'm sure we'll know more about settings.

1) Create a project, choose Application on the left, select Utility application on the right, click Next button

Enter appsettings in the product name and select Use storyboards, click the Next button

Click the Create button to complete the creation

Look at the project files that the program created for us automatically, there is a very common bidappdelegate, and we just selected the use Storyboard, So there is a mainstoryboard.storyboard existence, in addition to these, there are also 4 files, respectively, Bidmainviewcontroller and Bidflipsideviewcontroller, this is utility The application template is created automatically for us.

Since this is our first time to use Utility application, so here are some simple introduction, Utility application will automatically help us create 2 view, the first view is called Main view, The second view, called Flipside View, has a information button on the main view, and clicking on the information button switches to Flipside view. There is a done button on the Navigator bar of Flipside View, and clicking on the Done button will switch back to main view. In Project When you select Mainstoryboard.storyboard in Navigator, you will see 2 of the View, and the 2 view corresponds to Bidmainviewcontroller and Bidflipsideviewcontroller respectively.

OK, now we need to add a settings bundle, if you have an app that needs to be set up in Settings, then you have to add settings bundle to your project, and that's what works when you install your app on your iphone, It will automatically add an entry (cell) associated with this project to the settings for setting.

2) Add Settings Bundle
Select File>new>file ..., select resource on the left, select Settings Bundle on the right, click the Next button

Leave the default name and click the Create button to complete the add

Expand Settings.bundle, there are 2 default items, one is the En.lproj folder (this is temporarily ignored, the user localization program, now no need to ignore), and the other is root.plist, from the suffix we can conclude that the item in the settings is based on the property List, which is an XML file.

With the property list selected, the following will be displayed in Editor pane

Let's change the way root.list is displayed, right-click anywhere in the Editor pane and select "Show Raw keys/values" from the popup menu

Then the root.plist will appear like this

The most intuitive discovery is that the name of the 2 keys has changed, "Preference Items" became "Preferencespecifiers", "Strings Filename" became "stringstable", in fact, the content has not changed, Just show the way different, this to see a person like it, the former name more popular move, the latter name more close to the implementation of the situation.

In addition, we do not have to pay much attention to stringstable, this value is used in the localization, so you can delete it here directly.

3) Adding controls in the settings
Add text Field

Then we started Perferencespecifiers, which has 4 default item, which are generated by the system, but this is not what we want for this example, so we delete item 1, item 2, item 3, only the item 0.

(I also deleted the stringstable)

Then we expand item 0 to see what's inside.

There is a title and type key value pair (that is, dictionary), we focus on the type, its value is Psgroupspecifier, indicating that item is a group type, all the siblings after the item, belong to this group until the next group type appears. Under Preferencespecifiers, there must be at least one group present. The title is used to set the name of this group, which can be omitted.

Looking at the display of item 0: item 0 (Group-group), the first group in parentheses is the only type of item Psgroupspecifier, and the second group is the title value of the item. Let's change the value of the title and set it to "General Info"

Once setup is complete, you will notice that item 0 's display becomes: item 0 (group-general Info)

Psgroupspecifier is not an actual type to manipulate, it is simply to include several related attributes together to form a group in a table view, and we add the operational nodes below.

First, close item 0, then select the entire line of item 0, click the Return key on the keyboard, a new item (item 1) appears under item 0, and a list pops up to let you select the type of item.

Here we select text Field, after creating item 1, expand Item 1

You can see a total of 3 key-value pairs, type is Pstextfieldspecifier, the other 2 are title and key, assign title to "Username", assign key to "Username" (note case). Here the key is used to save and get the value of item 1, each item you can understand is a dictionary, a dictionary is a key value pair, then the function of this key is to get or save the specified value. (item 0 does not have a key, this is because the type of item 0 is psgroupspecifier, there is no need to save the value)

At the beginning of this article, we mentioned that Nsuserdefaultes,nsuserdefaultes is using this key to get the value of item, and also to save the updated value with this key.

Finally we add 2 properties for item 1, select the last line in item 1 (the row where key is located), then press RETURN on the keyboard, a new line appears under Key, and a list lets you select the key name, where we select " Autocapitalizationtype ", and then set the value to" None ". The same method is added again, this time select "Autocorrectiontype", and then set the value to "No"


"Autocapitalizationtype" means whether to automatically complete the input, that is, you enter a word in the first few words, the system will appear a list, the list will have the relevant vocabulary, you only have to select directly, you can complete the input, here we set to "None", No automatic completion is required.
"Autocorrectiontype" means to automatically correct the spelling, that is, the system will help you build the word you entered whether there is a spelling error, we also turned off the function.

OK, save the root.plist.

To this end, we can try to compile and run the program, in order to make the program can be highlighted in settings (find a little), we add an icon for the program, first download an icon, and then select the root node in Project Navigator appsettings, Then on the left, select the appsettings under targets, open the Summary tab, expand Iphone/ipod Deployment Info, find the app Icons

Drag the icon into the icons on the left (on the right is the Retina screen icon, we do not provide this icon, so it is empty)

OK, compile and run the program, after the program starts, press the home button back to the desktop, and then into the settings, at the bottom of the settings main interface, you can find the appsettings program icon we created

Click on the icon to enter the settings for this program

We can see in the root.plist that we set the item to restore the result, first is a group,group title for general Info, and then in group is a TextField item, its title is username.

OK, to this location, you should have a good understanding of settings, know how the items are generated, the role of each item's properties and so on. Next we'll add more different types of items, and we'll take a full settings operation.

3) Add additional items
Add Secure Text Field
Close Item 1, then select it, press command+c,command+v on the keyboard, copy and paste an item, and a new item 2 will appear under item 1.

Expand Item 2, and set the properties below it to look like this

As shown, Item 2 is used to receive a password, its type is pstextfieldspecifier, its title is set to Password,key to password, there is a new property called Issecure, When you set its value to Yes, the content in the text box is displayed as a password. (You can compile and run it to see if it is the effect of this password box)

Add Multivalue Field
Multivalue field will generate a cell with an arrow, click on the row, will jump to the next table view, the next table view will contain several options, the user selects one of the several options, and then return to the previous view, The content in the cell is what the user chooses.

We close item 2, then select Item 2, press RETURN to create a new item 3, and select Item 3 as the type multi Value

Expand Item 3 To see that type is psmultivaluespecifier. Set the title to Protocol,key to protocol.

OK, below we will add a set of titles and Values,titles to save the display values for each option, and the values to save the ID of each option, they are one by one corresponding. To keep item 3 unfold, then select Item 3, press RETURN (select in the form of item expansion and press Retuan to add a subkey for the current item; Select the return key in the item closed form to add a sibling) and create a new item 3 subkey and a drop-down box appears to select the type of subkey, where we select titles.

Repeat the action above, then create a subkey of item 3 and select values.

Select titles, and then click the plus sign to add a subkey that is assigned an HTTP

Repeat this action and add the following values SMTP,NNTP,IMAP,POP3

Using the same method, add an item for the values, and the following is done

The difference between the values and the titles is that one is lowercase and one is uppercase, and of course, if you use all uppercase or lowercase, it's not a problem.

Compile and run a program

Enter the password in Passwrod, which shows a small dot. Protocol an arrow to the right of the item, click it to jump to the next view

Here is the content of the titles we just added, we randomly select a

Check the status of SMTP, and then click the appsettings button in the top left corner to return to the previous view

The SMTP you just selected appears to the right of the protocol item.

Add Toggle Switch Settings
Toggle switch is simple, it is a switch, you can choose to open or close, the purpose of this type is to set a bool value.

Close Item 3, then select, press RETURN, add an item 4, and set the type of item 4 to toggle Switch, expand Item 4, set the title to "Warp Drive", Key to "Warp", Change the value of DefaultValue to Yes

Okay, compile and run, a switch appears under protocol.

Add Slider Setting
Slider we already know that in settings, a slider can place a picture on its 2 end (but I find that there are not many examples of images placed on the iphone), and that the slider itself has no textual description, We also can't put a label in settings to tell the user what the slider is for, so the solution here is to add a new group and then add a text description to the group to tell the user what the slider is for.

Close Item 4, and select, press RETURN, add Item 5, select the type of item 5 as group, and set the title value to "Warp Factor"

Close Item 5, and select, press RETURN, add Item 6, select the type of item 6 slider, according to the property set item 6

Compile and run the program, the effect is as follows


Just now we said, the two ends of the slider can add a picture (the size of the picture is pixel), we now add, first download the picture here

Adding an image to a slider is a bit special, and we don't just drag the picture directly into Project Navigator and put it in the slider, settings doesn't provide us with such a method. We use another method to add, first in Project Navigator, right-click Settings.bundle, select Show in Finder

Right-click Settings.bundle in the Finder and select "Show Package Contents" (Show Packages Contents)

Then copy the 2 images into the package and see the 2 images in Project navigator.

Then open root.plist, add 2 items in item 6 max value image filename and min value image filename, assign rabbit to Max and assign a value to min turtle

Once again, 2 pictures appear on the left and right 2 sides of the slider.

Add a child Settings View
This means clicking a cell on the table view to jump to another settings view. After some of the above explanations, we are actually working on the plist, so we can infer that all settings are in one plist file, so if you want to jump to another settings view, that must contain another plist, according to this idea, Let's do the following.

We'll create another group, created under item 6, and named title

Then we add item 8 below item 7, and by the way, now we should set the item 8 type, but in the default drop-down box there is no type we need (child Pane), no hurry, we expand Item 8 (Here Make sure you choose Show Raw Keys/values), and then click the button whose type row is the last side of the Value column, a drop-down box appears

Select Pschildpanespecifier in the drop-down box so that the type of item 8 is a child pane.

Then set the title to "more Settings", the key is empty, because this is a navigation action item, we do not need to get its value, it does not require its key.

Then we select the last line key, press RETURN to add one, select File from the drop-down menu

We need to associate a plist file so that we can navigate to another settings view, download the more.plist here, remember how we added 2 images to the slider just now? In Project Navigator, right-click Settings.bundle and choose Show in Finder, right-click Setting.bundle in the Finder, select Show Package Contents, Copy the more.plist in so that More.plist is present in Project navigator.

Because the more.plist is ready to help us, because we do not need to do anything, direct use on it. Compiling the Run Program

Settings view of the bottom more we just added more Settings, click More Settings, jump to more.plist Settings view

You can see the top 4 in the more in the text field, the last one is Mulitvalue field, then everyone casually, anyway, these things.

4) Summary
So far, all the controls that can be added in settings have been introduced, the total class is not many, altogether 6, and for settings operation, that is, the operation of a plist file, the iOS system will automatically reflect the contents of plist to Settings up, We can simply complete the settings view setting for the plist operation. In the next article, we will connect the value in settings with the real app, set the value in the settings, react in the app, change a value in the app, and the value in settings will be updated. The bridge connecting these two things is the nsuserdefaults mentioned earlier, we are in the next article in detail, thank you!

AppSettings

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.