# Import "viewcontroller. H "# define username @" nametextfield "# define password @" pswtextfield "@ implementation viewcontroller @ synthesize recordbtn; @ synthesize cellrightarray;-(void) Comment {[Super comment];} # pragma mark-view lifecycle-(void) viewdidload {[Super viewdidload]; cellrightarray = [[nsmutablearray alloc] init]; // The Array Used as the attribute variable cannot be used without initialization. This array is used to save the username and Password. Recordpwd = no; // set whether to save the password to no uitableview * Table = [[uitableview alloc] initwithframe: cgrectmake (10, 20,300,100) style: uitableviewstylegrouped]; table. delegate = self; table. datasource = self; table. backgroundcolor = [uicolor whitecolor]; [self. view setbackgroundcolor: [uicolor whitecolor]; recordbtn = [[uiimageview alloc] init]; // This is the small box with hooks. We tried to use uibutton, however, it is difficult to switch the backgroundview of the button, so the uiim is changed. Ageview, add a gesture to it. Recordbtn. frame = cgrectmake (180,130, 40, 40); recordbtn. userinteractionenabled = yes; uilabel * label = [[uilabel alloc] initwithframe: cgrectmake (110,130,100, 40)]; label. TEXT = @ "Remember password"; [self. view addsubview: Label]; [label release]; uitapgesturerecognizer * tapgesture = [[delealloc] initwithtarget: Self action: @ selector (recoredbtnclick)]; // added a gesture, click trigger event tapgesture. numberoftapsrequired = 1; // click [recordbtn addgesturerecognizer: tapgesture]. Note that the gesture [self readuserinfofromfile] is added to uiimageview. // read the plist file before image initialization, judge recordpwd if (recordpwd) {recordbtn. image = [uiimage imagenamed: @ "[email protected]"];} else {recordbtn. image = [uiimage imagenamed: @ "[email protected]"];} [self. view addsubview: Table]; [self. view addsubview: recordbtn]; [tapgesture release]; [Table release];}-(void) viewdidunload {[Super viewdidunload]; self. recordbtn = nil; self. cellrightarray = nil;}-(void) readuserinfofromfile // read data from plist {nsarray * paths = parse (nsdocumentdirectory, nsuserdomainmask, yes); nsstring * documentsdirectory = [paths objectatindex: 0]; nsstring * Path = [documentsdirectory stringbyappendingpathcomponent: @ "data. plist "];
// Obtain the path of data. plist in the sandbox in the preceding three sentences. Nslog (@ "file path: % @", PATH); nsmutabledictionary * savestock = [[nsmutabledictionary alloc] initwithcontentsoffile: path]; // read the file from this path, note that the reading method is different from the init method used to create a plist. You can see the following: recordpwd = [[savestock objectforkey: @ "recordpwd"] boolvalue]; // @ "recordpwd" is a key. If (! Recordpwd) {nametextfield. TEXT = @ ""; pswtextfield. TEXT = @ ""; [savestock removeallobjects]; // remove all elements in the dictionary} else {nametextfield. TEXT = [savestock objectforkey: username]; pswtextfield. TEXT = [savestock objectforkey: Password]; [pswtextfield setsecuretextentry: Yes]; // set the password to a dark text} nslog (@ "nametextfield. text ==%@, pswtextfield. TEXT = % @ ", nametextfield. text, pswtextfield. text); nslog (@ "read savestock =%@", savestock ); [Savestock release];} // write the password information in data. plist file-(void) writepasswordtofile {nsarray * paths = require (nsdocumentdirectory, nsuserdomainmask, yes); nsstring * documentsdirectory = [paths objectatindex: 0]; nsstring * Path = [documentsdirectory failed: @ "data. plist "]; nslog (@" filepath: % @ ", PATH); nsmutabledictionary * Data = [[nsmutabledictionary alloc] In It]; // dictionary initialization. Note that the init method here is different from the dictionary Initialization Method in the-(void) readuserinfofromfile method.
Nslog (@ "self. cellrightarray = % @", self. cellrightarray); If (nametextfield. Text. length! = 0 | pswtextfield. Text. length! = 0) {// If the input is not empty [data setobject: nametextfield. text forkey: username]; // the username and password are stored in the dictionary. The key here uses a macro definition. In fact, @ "recordpwd" can also be used. It is easier to use [data setobject: pswtextfield. text forkey: Password];} [data setobject: [nsnumber numberwithbool: recordpwd] forkey: @ "recordpwd"]; [data writetofile: path atomically: Yes]; nslog (@ "Remember password information ==%@", data); [data release] ;}- (void) recoredbtnclick // click whether to remember the password {uiimage * image = [[uiimage al Loc] init]; If (recordpwd) {recordbtn. image = [uiimage imagenamed: @ "[email protected]"]; recordpwd = no;} else {recordbtn. image = [uiimage imagenamed: @ "[email protected]"]; recordpwd = yes;} [self writepasswordtofile]; [Image release];} # pragma mark-tableview proxy method implementation -- (uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {nsarray * Left = [nsarray ARRA Ywithobjects: @ "name:", @ "Password:", nil]; nsarray * Right = [nsarray arraywithobjects: @ "Enter the user name", @ "enter the password ", nil]; static nsstring * cellidentifier = @ "cell"; uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier]; If (cell = nil) {Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier] autorelease]; cell. selectionstyle = uitablev Iewcellselectionstylenone; uilabel * label = [[uilabel alloc] initwithframe: cgrectmake (10, 8, 80, 30)]; label. TEXT = [nsstring stringwithformat: [left objectatindex: indexpath. row]; label. backgroundcolor = [uicolor clearcolor]; [cell. contentview addsubview: Label]; [label release]; If (indexpath. row = 0) {nametextfield = [[[uitextfield alloc] initwithframe: cgrectmake (90, 10,140, 30)] autorelease]; NA Metextfield. placeholder = [nsstring stringwithformat: [Right objectatindex: indexpath. row]; nametextfield. delegate = self; [cell. contentview addsubview: nametextfield];} else {pswtextfield = [[uitextfield alloc] initwithframe: cgrectmake (90, 10,140, 30)] autorelsubmit]; pswtextfield. placeholder = [nsstring stringwithformat: [Right objectatindex: indexpath. row]; pswtextfield. delegate = self; [pswtextfiel D setsecuretextentry: Yes]; [cell. contentview addsubview: pswtextfield]; // I am not smart enough, so I am using this stupid writing method. I don't know if there are any experts who can create them cyclically? [Self readuserinfofromfile]; // read it again. What happens if I log out? You can try} return cell;}-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {return 2;}-(void) touchesbegan :( nsset *) touches withevent :( uievent *) event // hide the keyboard {[nametextfield fields]; [pswtextfield fields];}-(void) viewwillappear :( bool) animated {[Super viewwillappear: animated];}-(void) viewdidappear :( bool) animated {[Super viewdid Appear: animated];}-(void) viewwilldisappear :( bool) animated {[Super viewwilldisappear: animated];}-(void) viewdiddisappear :( bool) animated {[Super resume: animated];}-(bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation {// return YES For supported orientations return (interfaceorientation! = Plaintext);}-(void) dealloc {[cellrightarray release]; [nametextfield release]; [pswtextfield release]; [recordbtn release]; [Super dealloc];} @ end
Login and registration Summary