iOS project development--use the user Preferences data API to store information

Source: Internet
Author: User

Android-developed students know that the content Provider, one of the four components of Android, is a lightweight data storage component that can store some simple configuration information or user information. For example, the most common place is to remember the user name password, so that the next time users open the application can no longer input, convenient operation. Coming to iOS, Apple also provides api--user preference data for similar functions for storing simple user configuration information, practical, convenient, and efficient. The specific implementation steps are as follows:

(1) Drag an input text box TextView and save button in Main.storyboard, wait for the content in the text box to be stored, and display it the next time you open the app. And the TextView is outlet bound, the button is the action binding.


(2) The implementation in the code is as follows:

Import Uikitclass Viewcontroller:uiviewcontroller {    @IBOutlet weak var inputtext:uitextview!        var ud:nsuserdefaults!   declaring user preferences;        override Func Viewdidload () {        super.viewdidload ()        ud = nsuserdefaults.standarduserdefaults ()                //Returns the Anyobject nullable type, so make an if let judgment, and if not empty, fill in the text box; if lets        uudd:anyobject = Ud.objectforkey ("data") {                    Inputtext.text = Uudd as! String        }                    }    @IBAction func Savebutton (Sender:uibutton) {                ud.setobject (inputtext.text, Forkey: " Data ")//click the button to store the contents of the text box, the key is" data "        println (" Saved ")    }    override Func didreceivememorywarning () {        super.didreceivememorywarning ()        //Dispose of any resources, can be recreated.    }}

(3) The program runs as follows:

First, save a piece of text:

。


Then quit the program, reopen the program, and found that the text can be displayed on the interface:

。


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS project development--use the user Preferences data API to store information

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.