#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];
//先读取plist文件 然后再写入,否则每次会将之前的数据给覆盖
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 = @"请输入姓名";
[self.view addSubview:_personName];
_phoneNumber.frame = CGRectMake(10, 114, 300, 30);
_phoneNumber.textAlignment = NSTextAlignmentLeft;
_phoneNumber.font = [UIFont boldSystemFontOfSize:13];
_phoneNumber.placeholder = @"请输入电话号码";
[self.view addSubview:_phoneNumber];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50, 160, 200, 30);
[btn setTitle:@"保存" 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(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(@"数据已保存");
//[_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:@"新建+" 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];
}
//读取数据并显示
-(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(@"新建");
}
-(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 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 @"电话本";
}
-(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.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 = @"请输入姓名";
_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 = @"请输入电话号码";
_phoneNumber.text = _number;
[self.view addSubview:_phoneNumber];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50, 160, 200, 30);
[btn setTitle:@"更改数据" 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);
*/
//先读取plist文件 然后再写入,否则每次会将之前的数据给覆盖
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(@"更改数据");
}
- (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