#import <UIKit/UIKit.h> #import "SubViewController.h" @interface viewcontroller:uiviewcontroller< Uitableviewdatasource,uitableviewdelegate,subviewcontrollerdelegate> @end
#import "SubViewController.h" @interface Subviewcontroller () @end @implementation subviewcontroller-(void) viewdidload {[Super viewdidload]; Self.view.backgroundColor = [Uicolor Whitecolor]; Read the plist file first and then write it, or each time the previous data is overwritten nsdictionary * dic = [[Nsdictionary alloc]initwithcontentsoffile:[nsstring Stringwit hformat:@ "%@/documents/demo.plist", Nshomedirectory ()]; NSLog (@ "++++++++++++%@", DIC); _infodict = [[Nsmutabledictionary alloc]initwithdictionary:dic]; _personname = [[Uitextfield alloc]init]; _personname.borderstyle = Uitextborderstyleline; _PhoneNumber = [[Uitextfield alloc]init]; _phonenumber.borderstyle = Uitextborderstyleline; _personname.delegate = self; _phonenumber.delegate = self; _personname.frame = CGRectMake (10, 74, 300, 30); _personname.textalignment = Nstextalignmentleft; _personname.font = [Uifont boldsystemfontofsize:13]; _personname.placeholder = @ "Please enter name"; [Self.view Addsubview:_personname]; _phonenUmber.frame = CGRectMake (10, 114, 300, 30); _phonenumber.textalignment = Nstextalignmentleft; _phonenumber.font = [Uifont boldsystemfontofsize:13]; _phonenumber.placeholder = @ "Please enter phone number"; [Self.view Addsubview:_phonenumber]; UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom]; Btn.frame = CGRectMake (50, 160, 200, 30); [Btn settitle:@ "Save" forstate:uicontrolstatenormal]; [btn Settitlecolor:[uicolor Cyancolor] forstate:uicontrolstatenormal]; [Btn addtarget:self Action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside]; Btn.tag = 1; [Self.view ADDSUBVIEW:BTN]; Do any additional setup after loading the view.} -(void) Btnclick: (UIButton *) btn{if (btn.tag==1) {Nsarray *path = Nssearchpathfordirectoriesindomains (nsdocumen Tdirectory, Nsuserdomainmask, YES); NSString *documentspath = [path objectatindex:0]; NSString *plistpath = [Documentspath stringbyappendingpathcomponent:@ "Demo.plist"]; [_infodicT Setobject:_phonenumber.text Forkey:_personname.text]; [_infodict Writetofile:plistpath Atomically:yes]; NSLog (@ "%@", Plistpath); NSLog (@ "Data saved"); [_infodict removeallobjects]; [_delegate insertinformation:_infodict]; }}-(BOOL) Textfieldshouldclear: (Uitextfield *) textfield{return YES; -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [SEG UE Destinationviewcontroller]. Pass the selected object to the new view Controller.} */@end
#import <UIKit/UIKit.h> @protocol subviewcontrollerdelegate <nsobject>-(void) Insertinformation: ( Nsmutabledictionary *) dcit, @end @interface subviewcontroller:uiviewcontroller<uitextfielddelegate> @property (nonatomic,weak) __weak id<subviewcontrollerdelegate>delegate; @property (nonatomic,strong) UITextField * PersonName, @property (nonatomic,strong) Uitextfield *phonenumber; @property (nonatomic,strong) nsmutabledictionary * infodict; @end
#import "ViewController.h" #import "NextViewController.h" @interface Viewcontroller () {Nsmutablearray *_data; UITableView *_tableview; Subviewcontroller *_subviewcontroller; Nextviewcontroller *_nextviewcontroller;} @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload]; _data = [[Nsmutablearray alloc]init]; _nextviewcontroller = [[Nextviewcontroller alloc]init]; Self.automaticallyadjustsscrollviewinsets = NO; _subviewcontroller = [[Subviewcontroller alloc]init]; _tableview = [[UITableView alloc]init]; _tableview.frame = CGRectMake (0, 64, 320, 416); _tableview.delegate = self; _tableview.datasource = self; _subviewcontroller.delegate = self; [Self.view Addsubview:_tableview]; Self.navigationItem.rightBarButtonItem = Self.editbuttonitem; Uibarbuttonitem *leftbbi = [[Uibarbuttonitem alloc]initwithtitle:@ "New +" Style:uibarbuttonitemstyledone target:self Action: @selector (Bbiclick)]; Self.navigationItem.righTbarbuttonitem = Leftbbi; Uibarbuttonitem *backbbi = [[Uibarbuttonitem alloc] initwithtitle:@ "Back" Style:uibarbuttonitemstyledone Target:nil Action:nil]; Self.navigationItem.backBarButtonItem = Backbbi; Additional setup after loading the view, typically from a nib. [Self readinformation]; [Self readinformation]; [_tableview Reloaddata];} -(void) Viewwillappear: (BOOL) animated{[self readinformation];} Read data and display-(void) readinformation{nsdictionary * dic = [[Nsdictionary alloc]initwithcontentsoffile:[nsstring stringwithformat:@ "%@/documents/demo.plist", Nshomedirectory ()]; _data = [[Nsmutablearray alloc]init]; [_data Addobject:dic]; NSLog (@ "*****************%@", _data);} -(void) Insertinformation: (nsmutabledictionary *) dcit{//nsdictionary * dic = [[Nsdictionary alloc] Initwithcontentsoffile:[nsstring stringwithformat:@ "%@/documents/demo.plist", Nshomedirectory ()]; [_data addobject:dcit]; _data = [[Nsmutablearray alloc]initwithobjeCts:dcit, nil]; NSLog (@ "1111%@", _data);} -(void) bbiclick{[Self.navigationcontroller Pushviewcontroller:_subviewcontroller Animated:yes]; NSLog (@ "new");} -(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return [_data count];} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return [[_data Objectatind Ex:section]count];} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static NSString *cellname [email protected] "cell"; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellname]; if (cell = = nil) {cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:celln AME]; }//cell.textlabel.text = @ "123"; Nsmutabledictionary *dic = [_data objectatindex:0]; Nsarray *array = [dic AllKeys]; for (int i = 0; i< array.count;i++) {//nsstring *str = [dic objectforkey:@]123 "]; Cell.textLabel.text = Allkeys[i]; }//Cell.textLabel.text = Array[i]; } Nsarray *array1 = [dic allvalues]; Cell.textLabel.text = [array ObjectAtIndex:indexPath.row]; _nextviewcontroller.phonenumber.text = Cell.textLabel.text; Cell.detailTextLabel.text = [Array1 objectAtIndex:indexPath.row]; _nextviewcontroller.personname.text = Cell.textLabel.text; return cell;} -(void) TableView: (UITableView *) TableView Accessorybuttontappedforrowwithindexpath: (Nsindexpath *) indexpath{}-( void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{_nextviewcontroller = [[ Nextviewcontroller Alloc]init]; Nsmutabledictionary *dic = [_data objectatindex:0]; Nsarray *array = [dic AllKeys]; for (int i = 0; i< array.count;i++) {//nsstring *str = [dic objectforkey:@ "123"]; Cell.textLabel.text = Allkeys[i]; }//Cell.textLabel.text = Array[i]; } Nsarray *array1 = [dic allvAlues]; _nextviewcontroller.name = [array ObjectAtIndex:indexPath.row]; NSLog (@ "1111111111%@", _nextviewcontroller.name); _nextviewcontroller.number = [Array1 objectAtIndex:indexPath.row]; NSLog (@ "2222222222%@", _nextviewcontroller.number); Nextviewcontroller *svc = [[Nextviewcontroller alloc]init]; [Self.navigationcontroller Pushviewcontroller:_nextviewcontroller animated:yes];} -(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{return @ "phone book";} -(void) setediting: (BOOL) editing animated: (BOOL) animated{[Super setediting:editing animated:animated]; [_tableview setediting:editing animated:animated];} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
#import <UIKit/UIKit.h> @interface nextviewcontroller:uiviewcontroller<uitextfielddelegate> @property ( Nonatomic,strong) Uitextfield *personname, @property (nonatomic,strong) Uitextfield *phonenumber; @property (nonatomic , strong) NSString *name, @property (nonatomic,strong) nsstring *number; @end
#import "NextViewController.h" @interface Nextviewcontroller () @end @implementation nextviewcontroller-(void) viewdidload {[Super viewdidload]; Self.view.backgroundColor = [Uicolor Whitecolor]; [Self. Nextviewcontroller Setnavigationbarhidden:yes]; _personname = [[Uitextfield alloc]init]; _personname.borderstyle = Uitextborderstyleline; _PhoneNumber = [[Uitextfield alloc]init]; _phonenumber.borderstyle = Uitextborderstyleline; _personname.delegate = self; _phonenumber.delegate = self; _personname.frame = CGRectMake (10, 74, 300, 30); _personname.textalignment = Nstextalignmentleft; _personname.font = [Uifont boldsystemfontofsize:13]; _personname.placeholder = @ "Please enter name"; _personname.text = _name; NSLog (@ "%@00000", _name); [Self.view Addsubview:_personname]; NSLog (@ "%@00000", _number); _phonenumber.frame = CGRectMake (10, 114, 300, 30); _phonenumber.textalignment = Nstextalignmentleft; _phonenumber.font = [Uifont boldsystEMFONTOFSIZE:13]; _phonenumber.placeholder = @ "Please enter phone number"; _phonenumber.text = _number; [Self.view Addsubview:_phonenumber]; UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom]; Btn.frame = CGRectMake (50, 160, 200, 30); [BTN settitle:@ "Change data" forstate:uicontrolstatenormal]; [btn Settitlecolor:[uicolor Cyancolor] forstate:uicontrolstatenormal]; [Btn addtarget:self Action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside]; Btn.tag = 1; [Self.view ADDSUBVIEW:BTN]; Do any additional setup after loading the view.} -(void) Btnclick: (UIButton *) btn; {if (btn.tag==1) {if (![ _personname.text isequaltostring:_name]| |! [_phonenumber.text Isequaltostring:_number]) {/* NSString *str = [NSString stringwithformat:@ "%@/document/demo.plist", Nshomedirectory ()] ; NSLog (@ "%@", Nshomedirectory ()); Nsdictionary * dic = [[Nsdictionary alloc]initwithcontentsoffile:str]; NSLog (@ "%@", dic); Nsmutabledictionary *dict = [[Nsmutabledictionary alloc]initwithdictionary:dic]; NSLog (@ "----------------%@", DIC); [Dict Removeobjectforkey:_name]; [Dict Setvalue:_phonenumber.text Forkey:_personname.text]; [Dict writetofile:str Atomically:yes]; NSLog (@ "%@", dict); *////Read the plist file before writing, otherwise overwrite the previous data with nsdictionary * dic = [[Nsdictionary alloc]initwithcontentsoffil E:[nsstring stringwithformat:@ "%@/documents/demo.plist", Nshomedirectory ()]; NSString *str = [NSString stringwithformat:@ "%@/documents/demo.plist", Nshomedirectory ()]; NSLog (@ "%@--------", DIC); Nsmutabledictionary *dict = [[Nsmutabledictionary alloc]initwithdictionary:dic]; [Dict Removeobjectforkey:_name]; [Dict Setvalue:_phonenumber.text Forkey:_personname.text]; NSLog (@ "$$$$$$$$$%@", dict); NSLog (@ "%@", Nshomedirectory ()); [Dict WRitetofile:str Atomically:yes]; [Self.navigationcontroller Popoverpresentationcontroller]; [Self.navigationcontroller Poptorootviewcontrolleranimated:yes]; [Self.navigationcontroller Popoverpresentationcontroller]; [Self.navigationcontroller Popviewcontrolleanimated:yes]; [Self.navigationcontroller Poptorootviewcontrolleranimated:yes]; }} NSLog (@ "change data");} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [SEG UE Destinationviewcontroller]. Pass the selected object to the new view Controller.} */@end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Design and implementation of iOS development-phone book