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