iOS development-Data Persistence Swift Chapter &settingbundle

Source: Internet
Author: User

Settingbundle

1 Import UIKit2 3 classViewcontroller:uiviewcontroller {4     5var Userdefault =nsuserdefaults.standarduserdefaults ()6     7@IBOutlet weak var textfield:uitextfield!8     9 @IBAction func btnsave (Sender:uibutton) {Ten updateuserdefaults () One     } A      - @IBAction func btnLoad (Sender:uibutton) { -var text = Userdefault.stringforkey ("TextField") theTextfield.text =text -     } -      -     Overridefunc viewdidload () { + super.viewdidload () -          +         /** A To set up an app: at Create a new file: The setting bundle in resource is opened directly inside the settings after it-----run. Pull it to the end, long enough to see -         */ -         /** - Okay, here's an explanation for the Settings.bundle: -          - in the Settings.bundle, En.iproj is a multi-lingual, temporary regardless. Our main concern is the Root.plist file. This file determines how the data we store is displayed in the Settings menu, and it has 7 types: in          - Group--group. The key is Psgroupspecifier, and the title of the preference logical group.  to text field--textbox. The key is Pstextfieldspecifier, an editable text string.  + title--caption. The key is Pstitlevaluespecifier, a read-only text string.  - Toggle Switch--switches. Key is pstoggleswitchspecifier, switch button.  the Slide--slider. The key is Pssliderspecifier, and the value is in a specific range of sliders.  * multivalue-Multi-value. The key is Psmultivaluespecifier, drop-down list.  $ Child Pane--sub-pane. The key is Pschildpanespecifier, child preference page. Panax Notoginseng          - some types of specific property descriptions: the          + Text Field A          the text Field is secure--whether it is a secure text. If set to Yes, the content appears as a dot symbol.  +          - autocapitalization Style--automatic capitalization. There are four values: none (None), sentences (the first letter of the sentence), Words (capitalize the first letter of the word), all characters (capitalization of all letters).  $          $ autocorrection Style-automatic correction of spelling, if turned on, you enter a non-existent word, the system will draw the red line hint. There are three values: default, no AutoCorrection (not automatically corrected), AutoCorrection (auto correct).  -          - Keyboard Type--keyboard style. There are five values: Alphabet (alphabet, default), Numbers and punctuation (numbers and punctuation), number Pad (digital panel), URL (more than Alphabet. com and other domain name suffixes), Email Address ( than Alphabet more than the @ match).  the          - Toggle SwitchWuyi          the value for on-the string value that is obtained when the switch is set to ON.  -          Wu value for off-the string value that is obtained when the switch is set to OFF.  -          About Slider $          - Minimum Value--minimum, number type.  -          - Maximum Value--Maximum, number type.  A          + min Value Image Filename--the picture at the end of the minimum.  the          - Max value Image Filename--the picture at the end of the maximum.  $          the P.S. The image size must be 21*21 and placed in the Settings.bundle package (display the package contents in the Finder and paste).  the          the multivalue the          - Values -a collection of value.  in          the titles-a collection of titles, corresponding to the value one by one.  the          About Child Pane the          the filename--The file name of the child plist.  the The Item0 here is very important, the type of ITEM0 is group, corresponds to a grouping table type, and the following item is in this grouping table until the next group is encountered. (That is, a group in figure two, you need to add the next group will be joined group type) +          - in our example: (Set a group, the first entry is the input name of the edit box, the second item is the switch variable of the yes/no, the third is the slide of the slider) the         Bayi Item0: Key setting Type:group, display title: Group.  the          the Item1: Key Settings Type:text Field, Title:name, identifier:name_preference, set Identifier to find data by Objectforkey.  -         */ -          the loaddefaults () the          the getuserdefaults () the          -     } the      the     //Load Default program-related application settings the func loaddefaults () {94         //setting Bundle thevar settingbundle = Nsbundle.mainbundle (). Pathforresource ("Settings", OfType:"Bundle") the         ifSettingbundle = =Nil { the             return98         } About          -         //Root101var root = Nsdictionary (contentsoffile:settingbundle!. stringByAppendingPathComponent ("root.plist"))102         //get a value in a dictionary103var prefrences:anyobject? = root?. Objectforkey ("preferencespecifiers") as? Array<nsdictionary>104         //put the dictionary contents of the array into a dictionary package thevar defaultstoregister = nsmutabledictionary (capacity:root!. Count)106         107          forPrefrenceinchprefrences {//' Anyobject? ' dose not having a member named ' Generator '108var key = Prefrence.objectforkey ("Key") asString?109             ifKey! =Nil { theDefaultstoregister.setvalue (Prefrences.objectforkey ("DefaultValue"), Forkey:key)111             } the         }113Nsuserdefaults.standarduserdefaults (). Registerdefaults (Defaultstoregister as[Nsobject:anyobject]) the     } the  the     Overridefunc didreceivememorywarning () {117 super.didreceivememorywarning ()118         //Dispose of any resources the can be recreated.119     } - 121     //Read122 func getuserdefaults () {123var defaults =nsuserdefaults.standarduserdefaults ()124var title = Defaults.objectforkey ("name_preference") as!String theTextfield.text =title126     }127      -     //Change system Settings129 func updateuserdefaults () { thevar defaults =nsuserdefaults.standarduserdefaults ()131Defaults.setbool (false, Forkey:"enabled_preference") the defaults.synchronize ()133     }134}

iOS development-Data Persistence Swift Chapter &settingbundle

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.