Sanshierli, started to learn IOS development from zero (nineteen): Application S

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

In iPhone and iPad, there is one thing everyone is familiar with, that is Settings


This is to learn something that is very simple, is to learn how to certain properties of a app is set in Settings, in turn, some property value changes in app, may also respond to Settings, this function is very common, the realization is relatively simple, but the content is still more.

First, or to Settings for a simple explanation, although we often open Settings, but rarely on the Settings were carefully studied, but as a IOS developer, there is this necessary to carry out a study of Settings Settings, have a look inside the package in the end with what, what are these things.

First we open the Settings, you can see the Settings interface (picture taken from the iPhone simulator)

As you can see, the Settings interface is a Table View, the Style attribute is Grouped. Besides the common General, Airplane Mode, Wi-Fi, Notifications (behind 3 not in the screenshot, open your iPhone, Settings there will be the option), iPhone for each there is a need for app to create a cell, such as Twitter, Facebook etc. Click on the Twitter, which can be seen the app contains a number of settings.

The settings in Settings are limited, it can contain the following 4 categories: Text, Field, Switch, Slider, TableView checklist.

We should see that, Settings is a Navigation Controller, between View and View is the inheritance relationship.

In the iOS is in what way to operate Settings, this thing called NSUserDefaults, it can easily interact with Settings in app, save the updated value.

Well, the simple introduction here, let us start to this example, as the example, I believe that there will be a deeper understanding of Settings.

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

Type AppSettings in the Product Name, and then select Use Storyboards, click the Next button

Click the Create button to complete the creation of

Look at the procedures for the project we automatically create the file, there is a very common BIDAppDelegate, we have selected Use Storyboard, so there is a MainStoryboard.storyboard, in addition to these, in addition to the 4 documents, which are BIDMainViewController and BIDFlipsideViewController, this is Utility Application template automatically created for us.

Since this is our first time to use Utility Application, so do some simple introduction here, Utility Application will automatically help us create 2 view, the first view called main view, second view called the flipside view, a information button in main view.

, Click on the information button will switch to the flipside view. There is a Done button in the flipside view navigator bar, click the Done button, it will switch back to main view. Select MainStoryboard.storyboard in the Project navigator, will see the 2 view, while the 2 view corresponding to BIDMainViewController and BIDFlipsideViewController.

Well, we need to add a Settings Bundle, if a app you need to be set in the Settings, you must add the Settings Bundle in your project, this thing is when you put your app installation to iPhone, it will automatically add an entry associated with this the project in Settings (cell) is used to set the.

2)Add Settings Bundle

File> New> File..., in the choice of Resource on the left, the right choice Settings Bundle, click the Next button

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

Settings.bundle, there are 2 default, one is the en.lproj folder (this ignored, user localization procedures, it should ignore), another is Root.plist, we can conclude from the suffix, items in the Settings is based on the Property list, is a XML file.

Select the Property list, show the following in the editor pane

We change the display mode of Root.list in the editor pane, right click anywhere, then in the pop-up menu“Show Raw Keys/Values”

Then the Root.plist will be displayed.

Find the most intuitive is the 2 Key of the name has changed, “ Preference Items” a “ PreferenceSpecifiers” “ Strings, Filename” a “ StringsTable”, in fact did not change, just show the different ways, the personal preferences, the former name of popular mobile, the latter name closer to the realization of.

In addition, we don't have too much attention to StringsTable, this value is used in the localization, so here you can delete it.

3)Add Settings controls

Add Text Field

Then we launched PerferenceSpecifiers, which has 4 default Item, these are the system helped us generate, but these are not our example, we want to delete, Item 1, Item 2, Item 3, only Item 0.

(the one I put StringsTable also deleted)

Then we launched Item 0, have a look what was inside.

There is a Title and Type key value pairs (also known as Dictionary), we focus on Type, its value is PSGroupSpecifier, the item is a group type, all the item in the item, belong to this group, group type until the next. In PreferenceSpecifiers, there must be at least one group. Title is used to set the name of the group, this property can be omitted.

In the look Item 0 display modes: Item 0 (Group - Group), in parentheses is only the first Group item is of type PSGroupSpecifier, second Group is the item Title value. We change the Title value, set the“General Info”

After setup completes, you'll find out Item 0 display into: Item 0 (Group - General Info)

Not a practical type PSGroupSpecifier and, its role is merely to several related properties including together, forming a table view in group, we add the node operable.

The Item 0 closed, then the mouse to select the Item line 0, return click on the keyboard, a new Item (Item 1) in Item 0 and below, there is a list of the pop-up, let you choose the Item type

Here we choose Text Field, created after Item 1, Item 1

You can see a total of 3 pairs, the Type type is PSTextFieldSpecifier, the other 2 were Title and Key, assigns Title to “ Username”, Key set to “ username” (case sensitive). Here Key is used to store and retrieve the value of Item 1, every Item you can be understood as a Dictionary, a Dictionary is a key value pair, then the Key function is available or save the specified value. (Item 0 is not Key, this is because the Item 0 is of type PSGroupSpecifier, no need to save the value)

In this article is just beginning, we mentioned NSUserDefaultes, NSUserDefaultes is to use the key to obtain the value of item, also use the key to save the updated value.

Finally, we Item 1 add 2 properties, select the Item 1 in the last line (Key line), and then press return on the keyboard, a new row in Key the following, in addition there is a list allows you to select the name of the Key, here we select “ AutocapitalizationType”, and then set values for the“None”. The same method added a second time, this time select “ AutocorrectionType”, and then set the value for“No”

“AutocapitalizationType”Mean whether input automatically, that is to say you type the first few characters of a word, the system will be a list appears, the list will be relevant vocabulary, you only direct election, we can complete the input, here we set to “ None”, without automatically.

“AutocorrectionType”Mean automatic spelling correction, is the system to help you build your input words spelling errors, we also turn this function off.

Well, save the Root.plist.

So far, we can try to compile and run the application, in order to make the program can display the prominent in Settings (to find a point), we add an icon for the program, first download a icon icon, select the root node in the Project and AppSettings navigator, then the left selection under TARGETS AppSettings, open the Summary tab iPhone / iPod Deployment, Info App, found Icons

Into the icon to the left of the icon (on the right is the retina screen icons, we here do not provide this icon, so empty)

Well, compile and run the program, the program starts, press the Home key to return to the desktop, and then enter the Settings, at the bottom of the main Settings interface, we can find to create AppSettings Icon

Click on the icon, it will enter the program settings

Above we can see we set an item in the Root.plist reduction out of the results, the first is a Group, Group Title General Info, then Group is a TextField, its Title is Username.

OK, To this location, you should have an understanding of the Settings, know the inside of the item is how to produce, the role of each item properties. We then add different types of more, comprehensive operation on Settings.

3)Add other items

Added Secure Text Field

The Item 1 closed, then select it, command+V press command+C on the keyboard,, copy and paste a copy of the Item, a new Item 2 will appear in the following Item 1

On Item 2, and its properties under settings for the following.

As shown above, Item 2 is used to receive the password, the Type type or PSTextFieldSpecifier, the Title is set to Password, Key to password, here a new attribute called IsSecure, when its value is set to YES, then it will be displayed in the password in the text box contents. (you can compile and run the. Have a look, is it right? The password box effect)

Add Multivalue Field

Multivalue Field produces a with an arrow cell, click on the row, will jump to the next table view, a table view can contain multiple options, a user selects one in a number of options, and then return to the previous view, the contents of the cell is to use the user selected content.

We are Item 2, then select Item 2, press the return key to the new Item 3, Item 3 type Multi Value

Item 3, you can see, Type is of type PSMultiValueSpecifier. Title is set to Protocol, Key is set to protocol.

ok, In the following we will add a group of Titles and Values, Titles to save the show every option value, each option ID to save Values, they were one one corresponding. Keep Item 3 carried forms, and then select Item 3, press the return key (in the Item expansion of the form select and press the retuan key, is to add child; current term in the Item closed form selected help by pressing the return key, is to add the line item), will create a new Item 3 sub item and the emergence of type a drop-down box to choose the subkey, here we choose Titles.

Repeat the above action, sub item to create a Item 3, and select Values.

Select Titles, and then click the plus sign, add an item, set to HTTP

Repeat this action, add the following value SMTP, NNTP, IMAP, POP3

Using the same method, Values add in, add after like the following

Values and Titles are different in one case an uppercase, of course, if you all uppercase or lowercase is not the problem, see a personal preference.

Compile and run a program

Enter the password in Passwrod, that is a small dot. Protocol right more than one arrow, click the item, jump to the next view

Here is what we just add the contents of Titles, we randomly selected a

Select the SMTP state, then we click on the upper left corner of the AppSettings button to return to the previous level view

Just select the SMTP is displayed in the Protocol item on the right.

Added Toggle Switch Settings

Toggle Switch is very simple, is a switch, can choose to open or closed, the type is designed to set a bool value.

Closure of Item 3, and then selected, press the return key, add a Item 4, and set for Toggle Item 4 Switch, Item 4, Title “ Warp Drive” Key “ warp”,, change the DefaultValue value to YES

Well, run the compiler, a Switch appears in the Protocol below

Add Slider Setting

Slider we have known, in Settings, a Slider in the 2 end of each placed a picture (but I found a place in the iPhone picture not many examples), Slider itself is not with text descriptions, what we can't put a label inform the user of the slider effect in Settings therefore, the solution here is to add a new Group, then add Group text, inform the user of the role of slider.

Closure of Item 4, and selected, press the return key, the addition of Item 5, Item 5 is of type Group, and set the Title value“Warp Factor”

Closure of Item 5, and selected, press the return key, the addition of Item 6, Item 6 is of type Slider, according to the property of the set Item 6

Compile and run the program, the effect is as follows

We have just said, Slider ends can all add a picture (picture size is 21 * 21 pixel), we have to add it, download the images here

Method to add image to slider some special, we are not directly to the image onto the project navigator, and then into the slider, Settings does not provide such methods as we. We use other methods to add, first in Project navigator mouse right click Settings.bundle, select Show in Finder

In Finder, right-click on the Settings.bundle, select “ show package contents”(Show Package Contents)

Then the 2 picture is copied into the bag, and then in the Project navigator also can see these 2 pictures.

Then open the Root.plist, adding 2 Max Value Image Filename and Min Value Image Filename Item 6, Max = rabbit, Min = turtle

To compile and run, 2 images appear in the slider around 2 sides.

Add a Child Settings View

This means that a cell click table on the view, jump to another Settings view. After some explanation of the above, we are actually working on the plist, it can be inferred that, all of the Settings are in a plist file, so if you want to jump to another Settings view, it must contain a plist, according to this thought, we carried out the following operations.

We will create a Group, created in Item 6 below, and named Title

Then we in Item 7 below to add Item 8, according to the way before, now we should set type Item 8, but in the default in the drop-down box and not of the type we need (Child Pane), don't worry, we expand the Item 8 (here to make sure you choose the Show Raw Keys/Values), and then click on the Type Value column at the end side of the button, will a drop-down box appears

Select PSChildPaneSpecifier from the drop-down box, so that the Item 8 type is a Child Pane.

Then set the Title to “ More, Settings” Key is empty, because it is a play the role of navigation Item, we do not need to get its value, also do not need it Key.

Then we select the last line Key, press the return button to add a, select File from the drop-down menu

An associated plist file we need, So that you can navigate to another Settings view., Download the More.plist here, Remember just how we add slider 2 pictures? In the Project navigator in the mouse right click on the Settings.bundle, Then select the Show in Finder, Right click on the Setting.bundle in Finder, Select Show Package Contents, Copy More.plist., In this way Project navigator appeared in More.plist.

Because More.plist is ready to help us do, because we don't need any of its operation, the direct use of it. Compile and run the application

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

You can see in More 4 is Text Field, the last one is Mulitvalue Field, then you just point to it, anyway, these things.


So far, all the controls can be added in Settings are introduced, the total is not many, a total of 6, and for Settings operation, which is a plist file operations, iOS system will automatically be content response in plist up to Settings, we only need to operate on the plist, is can be very simple to complete the Settings view settings. In the next chapter, we will bring value and real app application connected to Settings, set the value in Settings, in app will be reflected, to change a value in app, in Settings this value will also be updated, the bridge connecting these two things will is the previously mentioned NSUserDefaults, we are introduced in detail in the next chapter, thank you!


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lewis at November 13, 2013 - 7:56 PM