Ios development-Design and Implementation of the phone book and ios development phone book
#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 before writing it. Otherwise, the previous data is overwritten by NSDictionary * dic = [[NSDictionary alloc] initWithContentsOfFile: [NSString stringWithFormat: @ "% @/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 = @ "enter your name"; [self. view addSubview: _ personName]; _ phoneNumber. frame = CGRectMake (10,114,300, 30); _ phoneNumber. textAlignment = NSTextAlignmentLeft; _ phoneNumber. font = [UIFont boldSystemFontOfSize: 13]; _ phoneNumber. placeholder = @ "Enter the 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 = require (NSDocumentDirectory, 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 that can be recreated .} /* # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// Get the new view controller using [segue 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; // Do any 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 objectAtIndex: section] count];}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * cellName = @ "cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: cellName]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellName];} // 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 preview :( 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 @ "";}-(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 that 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. borderS Tyle = UITextBorderStyleLine; _ personName. delegate = self; _ phoneNumber. delegate = self; _ personName. frame = CGRectMake (10, 74,300, 30); _ personName. textAlignment = NSTextAlignmentLeft; _ personName. font = [UIFont boldSystemFontOfSize: 13]; _ personName. placeholder = @ "Enter name"; _ personName. text = _ name; NSLog (@ "%@ 00000", _ name); [self. view addSubview: _ personName]; NSLog (@ "% @ 00000", _ number); _ phoneNumb Er. frame = CGRectMake (10,114,300, 30); _ phoneNumber. textAlignment = NSTextAlignmentLeft; _ phoneNumber. font = [UIFont boldSystemFontOfSize: 13]; _ phoneNumber. placeholder = @ "Enter the 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: UIControlStateN Ormal]; [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 isw.tostring: _ 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 it, otherwise, the previous data will be overwritten by NSDictionary * dic = [[NSDictionary alloc] initWithContentsOfFile: [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 didreceivemorywarning]; // Dispose of any resources that can be recreated .} /* # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller .} * // @ end
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.