Use of nsuserdefaults in IOS

Source: Internet
Author: User

There are multiple methods for creating a user ults, which is the simplest and fastest way to create a user defaults:

NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];

Add data to user defaults:

[accountDefaults setObject:nameField.text forKey:UserDefaultNameKey];

You can also add basic data types such as int, float, and bool.

[accountDefaults setBool:YES forKey:UserDefaultBoolKey];

Get data from user defaults:

[accountDefaults objectForKey:NCUserDefaultNameKey]  [accountDefaults boolForKey: UserDefaultBoolKey];

Key points:

Nsuserults ults is very easy to use. You do not need to set the global variable of nsuserdefaults in the program. Where to use nsuserdefaults data, create an nsuserdefaults object and perform read or write operations.

You can rewrite the object or data corresponding to the same keyword. After rewriting, the keyword corresponds to a new object or data, and the old object or data is automatically cleared.


Here is an example by myself. You can use the checkbtn button to save the user name and password:


Image 1: the interface that is started for the second time before the data is saved or the data is not saved for the last time


Image 2: The data is saved, and the Initial Startup interface is displayed for the second time.

-(Void) defaultcheck :( ID) sender {nslog (@ "Check button pressed"); nsuserdefaults * accountdefaults = [nsuserdefaults standarduserdefaults]; If ([accountdefaults boolforkey: ncuserdefaultboolforregister] = No) {(uibutton *) sender ). selected = yes; [accountdefaults setbool: Yes forkey: ncuserdefaultboolforregister]; [accountdefaults setobject: namefield. text forkey: ncuserdefaultnamekey]; [accountdefaults setobjec T: pwdfield. text forkey: ncuserdefapasspasswordkey];} else {// Yes (uibutton *) sender ). selected = no; [accountdefaults setbool: No forkey: Counter]; [accountdefaults setobject: Nil forkey: Counter]; [accountdefaults setobject: Nil forkey: ncuserdefapasspasswordkey];}-(void) loadview {[Super loadview]; namefield = [[uitextfield alloc] initwithframe: cgrectmake (0, 0,100, 30 )]; Pwdfield = [[uitextfield alloc] initwithframe: cgrectmake (0, 0,100, 30)]; [namefield setcenter: cgpointmake (160,100)]; [pwdfield setcenter: cgpointmake (160,150)]; nsuserdefaults * accountdefaults = [nsuserdefaults standarduserdefaults]; // The bool value must be saved with boolforkey: Get // if the user defaults has been saved, when reading the data, the data will be read and displayed in the username and password boxes (not encrypted here ). If ([accountdefaults boolforkey: ncuserdefaboboolforregister] = Yes) {namefield. TEXT = [accountdefaults objectforkey: ncuserdefaultnamekey]; pwdfield. TEXT = [accountdefaults objectforkey: ncuserdefapasspasswordkey];} else {[namefield setplaceholder: @ "username"]; [pwdfield setplaceholder: @ "password"];} namefield. borderstyle = uitextborderstylebezel; [pwdfield setborderstyle: uitextborderstyleroundedrect]; [self. view addsubview: namefield]; [self. view addsubview: pwdfield]; uibutton * loginbtn = [uibutton buttonwithtype: Empty]; [loginbtn setframe: cgrectmake (0, 0, 50, 30)]; [loginbtn setcenter: cgpointmake (200,200)]; [loginbtn addtarget: Self action: @ selector (logtailweibo :) forcontrolevents: uicontroleventtouchupinside]; [self. view addsubview: loginbtn]; uibutton * checkbtn = [uibutton buttonwithtype: Allow]; [checkbtn setframe: cgrectmake (0, 0, 20, 20)]; [checkbtn setcenter: cgpointmake (160,180)]; // The selected icon [checkbtn failed: [uiimage imagenamed: @ "lock.png"] forstate :( uicontrolstate) Success]; [checkbtn setbackgroundimage: [uiimage imagenamed: @ "lock.png"] forstate :( uicontrolstate) uicontrolstateselected]; // [checkbtn failed: [uiimage imagenamed: @ "unlock.png"] forstate :( uicontrolstate) uicontrolstatenormal]; [checkbtn addtarget: self action: @ selector (defaultcheck :) forcontrolevents: uicontroleventtouchupinside]; // If ([accountdefaults boolforkey: ncuserdefaultboolforregister] = Yes) {[checkbtn setselected: yes];} [self. view addsubview: BTN];}

Article Source: http://xiaohui3837843.blog.163.com/blog/static/5438874020115823834836/


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.