IOS data persistence 1

Source: Internet
Author: User

-(Void) loadview

{

Uiview * aview = [[uiview alloc] initwithframe: cgrectmake (0, 0,320,480)];

Aview. backgroundcolor = [uicolor whitecolor];

Self. view = aview;

[Aview release];

T1 = [[uitextfield alloc] initwithframe: cgrectmake (80, 30, 80, 30)];

T1.borderstyle = uitextborderstyleroundedrect;

T1.text = @"";

[Aview addsubview: T1];

T2 = [[uitextfield alloc] initwithframe: cgrectmake (80,120, 80, 30)];

T2.borderstyle = uitextborderstyleroundedrect;

T2.text = @"";

[Aview addsubview: T2];

T3 = [[uitextfield alloc] initwithframe: cgrectmake (80,210, 80, 30)];

T3.borderstyle = uitextborderstyleroundedrect;

T3.text = @"";

[Aview addsubview: T3];

Nsfilemanager * fm = [nsfilemanager defamanager manager];

If ([FM fileexistsatpath: [self filepath]) //If any file exists, it is retrieved from the file.,Read from File

{

Nsarray * arr = [nsarray arraywithcontentsoffile: [self filepath];

T1.text = [arr objectatindex: 0];

T2.text = [arr objectatindex: 1];

T3.text = [arr objectatindex: 2];

}

Nslog (@ "% @", [self filepath]);

//Background running

[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (savedata) Name: uiapplicationdidenterbackgroundnotification object: Nil];

}

-(Void) savedata

{

Nsarray * Texts = [nsarray arraywithobjects: t1.text, t2.text, t3.text, nil];

[Texts writetofile: [self filepath] atomically: Yes]; //Store data in a file

}

-(Nsstring *) filepath

{

// DocumentPath

Nsstring * docpath = [nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) objectatindex: 0];

Nsstring * Path = [docpath stringbyappendingpathcomponent: @ "texs"];

ReturnPath;

}

Related Article

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.