Starting from scratch, I learned ios development (19th): Application Settings and User ults (previous

Source: Internet
Author: User

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

 

 

 

 

 

 

 

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.