ios开发-电话本的设计与实现

#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 [email protected]"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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-10 07:35:28

ios开发-电话本的设计与实现的相关文章

iOS UI_电话本

// // MainViewController.m // 电话本 // // Created by dllo on 15/8/7. // Copyright (c) 2015年 zhozhicheng. All rights reserved. // #import "MainViewController.h" #import "SecondViewController.h" #import "AddViewController.h" //4.

IOS开发之自动布局框架设计(二)

[上集剧情概要:上集我们主要剖析了原生的NSLayoutConstraint实现自动布局的方式,我们知道是通过constraintWithItem这个初始化的方法来配备所需要的7个参数,然后通过addConstraint方法将布局添加进去,并且定义了 NSLayoutAttribute,NSLayoutRelation这些枚举] 如果我们自己设计一款布局框架可以怎么设计呢? 1.封装原有的NSLayoutConstraint类,可以将代码的实现更加简洁化.例如:Masonry,SDAutoLay

IOS开发之代理的设计小技巧

1.关于代理对象的设计小技巧 在设计一个类,需要通过代理和协议来从外部获取需要的动态的数据.那么在这里设计使用代理会有两种方法. <第一种方法> 也是比较常见的: 在你设计的类中,声明一个代理属性 然后外部使用的时候 最后根据那个<...Protocol>协议,去遵循这个协议并实现协议的方法. <第二种方法>在创建这个你要设计的类对象的构造方法中添加一个代理对象的参数,目的就是按照需要,你如果要创建这个对象,你必须添加代理对象. 这样外部在创建这个对象的时候,使用这个方

(一)iOS开发--电子书模块的设计制作

一.引言 从今天开始,在我的博客上开辟工作项目专栏,来总结记录项目中的开发难点.第一篇记录的是电子书模块. 二.项目需求 一级界面:1.侧滑菜单.2.书籍列表.3.上拉加载 二级界面:1.头部书籍信息.2.书籍简介.3.评价列表.4.评价功能(弹出评价界面)5.底部下载/阅读功能 下载管理界面:1.下载的书籍信息.2.可侧滑删除 阅读界面:1.电子书自适应大小(pdf文件).2.翻页 .3.记录页 三.总结(后台拿到的测试数据) 1.一级界面 0.0 1.pid为1的为父类,sn进行排序的标志,

IOS开发之自动布局框架设计(三)

[上集剧情概要:上集我们主要试着用连式结构写了一个简单地布局的设计的demo,首先通过block方式实现链式调用,然后封装添加布局的约束到block里面,实现了上下左右的简单布局] 好吧,各位观众,接下来抛砖引玉,逐渐去添加一些布局功能的时候到了..... 首先,我们考虑一个问题,因为上集我们主要是默认相对视图为superview,而且都是用默认偏移量constant,并没有倍数关系,那么我们如何加入toItem和multiplier这两个参数呢??? 用什么方式展示给用户更为好的呢??? 思考

IOS开发之微博的设计与实现

// // main.m // Microblog // #import <Foundation/Foundation.h> #import "Person.h" #import "BlogMaster.h" #import "Microblog.h" int main(int argc, const char * argv[]) { Person * person = [[Person alloc]init]; [person sh

学习IOS开发UI篇--MVC设计界面及数据的模型转换

1.字典转模型 ======================================== 1.1字典转模型的好处: 1> 降低代码的耦合度 2> 所有字典转模型部分的代码统一集中在一处处理,降低代码出错的几率 3> 在程序中直接使用模型的属性操作,提高编码效率 模型应该提供一个可以传入字典参数的构造方法 - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)xxxWithDict:(NSDiction

IOS开发之自动布局框架设计(四)

[上集剧情概要:上集我们主要实现了一个完整的自动布局的框架,这集我们主要研究下比较流行的开源布局框架Masonry的布局思路] 我们先来看看是如何开始使用Masonry的,一般我们使用这个布局框架的时候,都会调用以下代码..... [self.view1 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(50); make.right.mas_equalTo(-50); make.top.mas_equ

iOS开发&gt;学无止境 - 浅谈MVVM的架构设计与团队协作

李刚按:本文是青玉伏案写的一篇文章.相信大家对MVC耳熟能详,MVVM可能听说的相对少一些,这一篇文章将会想你阐述MVVM设计,还有团队协作的经验分享.如果你也觉得不错,就分享一下吧! demo:https://github.com/lizelu/MVVM 今天写这篇文章是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇文章的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦~). 由 于本人项目经验有限,关于架构设