通讯录

//
//  MainViewController.m
//  通讯录
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "MainViewController.h"
#import "ChangeViewController.h"
#import "SaveViewController.h"
@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate,SaveViewController,ChangeViewController>


@property(nonatomic, retain)UITableView         *tableView;
@property(nonatomic, retain)NSMutableArray      *personArr;
@property(nonatomic, retain)NSIndexPath         *currentIndexPath;
@end

@implementation MainViewController
- (void)dealloc
{
    [_tableView release];
    [_personArr release];
    [_currentIndexPath release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self createData];
    }
    return self;
}

- (void)createData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"StudentArr" ofType:@"plist"];
    self.personArr = [[NSMutableArray alloc] initWithContentsOfFile:path];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.title = @"通讯录";
    
    // 添加联系人按钮
    self.navigationItem.rightBarButtonItem= [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"1.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)] autorelease];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.rowHeight = 100;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.personArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
    }
    cell.textLabel.text = self.personArr[indexPath.row][@"name"];
    cell.detailTextLabel.text = self.personArr[indexPath.row][@"phone"];
    NSString *picName = [NSString stringWithFormat:@"%ld.jpg", indexPath.row + 1];
    cell.imageView.image = [UIImage imageNamed:picName];
   return cell;
}

- (void)rightButtonAction:(UIBarButtonItem *)button
{
    SaveViewController *saveVC = [[SaveViewController alloc] init];
    /// 5.设置代理人
    saveVC.delegate = self;
    [self.navigationController pushViewController:saveVC animated:YES];
    [saveVC release];
}

/// 6.实现协议方法
- (void)addName:(NSString *)name Sex:(NSString *)sex Hobby:(NSString *)hobby Phone:(NSString *)phone Image:(NSString *)image
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:name, @"name", sex, @"sex", hobby, @"hobby", phone, @"phone", image, @"photo", nil];
    if (![dic[@"name"] isEqualToString:@""]) {
        [self.personArr addObject:dic];
        [self.personArr writeToFile:@"/Users/dllo/Desktop/StudentArr.plist" atomically:YES];
    }
    NSLog(@"%@", self.personArr.lastObject[@"name"]);
    [self.tableView reloadData];
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.currentIndexPath = indexPath;
    
    ChangeViewController *changeVC = [[ChangeViewController alloc] init];
    [self.navigationController pushViewController:changeVC animated:YES];
    changeVC.perDic = self.personArr[indexPath.row];
    changeVC.photoStr = [NSString stringWithFormat:@"%ld.jpg",indexPath.row + 1];
    changeVC.row = indexPath.row;
    changeVC.deleagte = self;
    [changeVC release];
}

- (void)changeName:(NSString *)name Sex:(NSString *)sex Hobby:(NSString *)hobby Phone:(NSString *)phone Image:(NSString *)image
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:name, @"name", sex, @"sex", hobby, @"hobby", phone, @"phone", image, @"photo", nil];
    self.personArr[self.currentIndexPath.row] = dic;
//    [self.tableView reloadData];
    NSArray *arrar  = [NSArray arrayWithObject:self.currentIndexPath];
    [self.tableView reloadRowsAtIndexPaths:arrar withRowAnimation: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
//
//  ChangeViewController.h
//  通讯录
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import <UIKit/UIKit.h>

// 1.声明一份协议
@protocol ChangeViewController <NSObject>

- (void)changeName:(NSString *)name
               Sex:(NSString *)sex
             Hobby:(NSString *)hobby
             Phone:(NSString *)phone
             Image:(NSString *)image;

@end
@interface ChangeViewController : UIViewController

// 2.设置代理人属性
@property(nonatomic, assign)id<ChangeViewController>deleagte;
@property(nonatomic, retain)NSMutableDictionary *perDic;
@property(nonatomic, copy)NSString *photoStr;
@property(nonatomic, assign)NSInteger row;

@end

//
//  ChangeViewController.m
//  通讯录
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "ChangeViewController.h"
#define HEIGHT self.view.frame.size.height
@interface ChangeViewController ()<UITextFieldDelegate>
@property(nonatomic, retain)UIButton *changeButton;
@property(nonatomic, retain)UITextField *nameTF;
@property(nonatomic, retain)UITextField *sexTF;
@property(nonatomic, retain)UITextField *hobbyTF;
@property(nonatomic, retain)UITextField *phoneTF;
@property(nonatomic, retain)UIImageView *image;
@end

@implementation ChangeViewController

- (void)dealloc
{
    [_perDic release];
    [_photoStr release];
    [_changeButton release];
    [_nameTF release];
    [_sexTF release];
    [_hobbyTF release];
    [_phoneTF release];
    [_image release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.photoStr]];
    self.image.backgroundColor = [UIColor lightGrayColor];
    self.image.frame = CGRectMake(100, 50, 200, 200);
    [self.view addSubview:self.image];
    [self.image release];
    
    UILabel *nameLable = [[UILabel alloc] initWithFrame:CGRectMake(100, 300, 50, 30)];
    nameLable.text = @"姓 名:";
    nameLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:nameLable];
    [nameLable release];
    
    self.nameTF = [[UITextField alloc] initWithFrame:CGRectMake(160, 300, 150, 30)];
    self.nameTF.text = self.perDic[@"name"];
    self.nameTF.backgroundColor = [UIColor lightGrayColor];
    self.nameTF.layer.borderWidth = 1;
    self.nameTF.layer.cornerRadius = 10;
    self.nameTF.layer.masksToBounds = YES;
    [self.view addSubview:self.nameTF];
    self.nameTF.clearsOnBeginEditing = YES;
    self.nameTF.delegate = self;
    [self.nameTF release];
    
    
    UILabel *sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 350, 50, 30)];
    sexLabel.text = @"性 别";
    sexLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:sexLabel];
    [sexLabel release];
    
    self.sexTF = [[UITextField alloc] initWithFrame:CGRectMake(160, 350, 150, 30)];
    self.sexTF.text = self.perDic[@"sex"];
    self.sexTF.backgroundColor = [UIColor lightGrayColor];
    self.sexTF.layer.borderWidth = 1;
    self.sexTF.layer.cornerRadius = 10;
    self.sexTF.layer.masksToBounds = YES;
    [self.view addSubview:self.sexTF];
    self.sexTF.clearsOnBeginEditing = YES;
    self.sexTF.delegate = self;
    [self.sexTF release];
    
    UILabel *hobbyLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 50, 30)];
    hobbyLabel.text = @"爱好";
    hobbyLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:hobbyLabel];
    [hobbyLabel release];
    
    self.hobbyTF = [[UITextField alloc] initWithFrame:CGRectMake(160, 400, 150, 30)];
    self.hobbyTF.text = self.perDic[@"hobby"];
    self.hobbyTF.backgroundColor = [UIColor lightGrayColor];
    self.hobbyTF.layer.borderWidth = 1;
    self.hobbyTF.layer.cornerRadius = 10;
    self.hobbyTF.layer.masksToBounds = YES;
    [self.view addSubview:self.hobbyTF];
    self.hobbyTF.clearsOnBeginEditing = YES;
    self.hobbyTF.delegate = self;
    [self.hobbyTF release];
    
    
    UILabel *phoneLable = [[UILabel alloc] initWithFrame:CGRectMake(100, 450, 50, 30)];
    phoneLable.text = @"电 话:";
    phoneLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:phoneLable];
    [phoneLable release];
    
    self.phoneTF = [[UITextField alloc] initWithFrame:CGRectMake(160, 450, 150, 30)];
    self.phoneTF.text = self.perDic[@"phone"];
    self.phoneTF.backgroundColor = [UIColor lightGrayColor];
    self.phoneTF.layer.borderWidth = 1;
    self.phoneTF.layer.cornerRadius = 10;
    self.phoneTF.layer.masksToBounds = YES;
    [self.view addSubview:self.phoneTF];
    self.phoneTF.clearsOnBeginEditing = YES;
    self.phoneTF.delegate = self;
    [self.phoneTF release];
    
    
    self.changeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.changeButton setTitle:@"编 辑" forState:UIControlStateNormal];
    self.changeButton.frame = CGRectMake(200, 500, 50, 40);
    [self.view addSubview:self.changeButton];
    [self.changeButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)click:(UIButton *)but
{
    [self.deleagte changeName:self.nameTF.text Sex:self.sexTF.text Hobby:self.hobbyTF.text Phone:self.phoneTF.text Image:self.photoStr];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nameTF resignFirstResponder];
    [self.sexTF resignFirstResponder];
    [self.hobbyTF resignFirstResponder];
    [self.phoneTF resignFirstResponder];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField.frame.origin.y > HEIGHT / 2) {
        CGFloat height = textField.frame.origin.y - HEIGHT / 2;
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y -height);
    }
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (textField.frame.origin.y > HEIGHT / 2) {
        CGFloat height = textField.frame.origin.y - HEIGHT / 2;
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y + height);
    }
    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

//
//  SaveViewController.h
//  通讯录
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import <UIKit/UIKit.h>

/// 声明一份协议
@protocol SaveViewController <NSObject>
- (void)addName:(NSString *)name
            Sex:(NSString *)sex
          Hobby:(NSString *)hobby
          Phone:(NSString *)phone
          Image:(NSString *)image;
@end

@interface SaveViewController : UIViewController

/// 2.设置代理人属性
@property(nonatomic, assign)id<SaveViewController>delegate;
@property(nonatomic, retain)NSMutableDictionary *dic;
@property(nonatomic, retain)NSString *photo;
@end

//
//  SaveViewController.m
//  通讯录
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "SaveViewController.h"
#import "ChangeViewController.h"
@interface SaveViewController ()<UITextFieldDelegate>
@property(nonatomic, retain)UITextField *nameTF;
@property(nonatomic, retain)UITextField *phoneTF;
@property(nonatomic, retain)UITextField *sexTF;
@property(nonatomic, retain)UITextField *hobbyTF;
@property(nonatomic, retain)UIButton *button;
@property(nonatomic, retain)UIImageView *image;
@property(nonatomic, retain)UIButton *imageButton;

@end

@implementation SaveViewController

- (void)dealloc
{
    [_dic release];
    [_photo release];
    [_nameTF release];
    [_phoneTF release];
    [_sexTF release];
    [_hobbyTF release];
    [_button release];
    [_image release];
    [_imageButton release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.photo]];
    self.image.frame = CGRectMake(30, 50, 100, 100);
    self.image.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.image];
    [self.image release];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(150, 50, 50, 30)];
    name.text = @"姓 名:";
    name.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:name];
    [name release];
    
    self.nameTF = [[UITextField alloc] initWithFrame:CGRectMake(210, 50, 150, 30)];
    [self.view addSubview:self.nameTF];
    [self.nameTF release];
    self.nameTF.text = self.dic[@"name"];
    self.nameTF.backgroundColor = [UIColor grayColor];
    self.nameTF.layer.borderWidth = 1;
    self.nameTF.layer.cornerRadius = 10;
    self.nameTF.layer.masksToBounds = YES;
    self.nameTF.clearsOnBeginEditing = YES;
    self.nameTF.delegate = self;
    
    UILabel *sex = [[UILabel alloc] initWithFrame:CGRectMake(150, 100, 50, 30)];
    sex.text = @"性 别:";
    sex.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:sex];
    [sex release];
    
    self.sexTF = [[UITextField alloc] initWithFrame:CGRectMake(210, 100, 150, 30)];
    [self.view addSubview:self.sexTF];
    [self.sexTF release];
    self.sexTF.text = self.dic[@"sex"];
    self.sexTF.backgroundColor = [UIColor grayColor];
    self.sexTF.layer.borderWidth = 1;
    self.sexTF.layer.cornerRadius = 10;
    self.sexTF.layer.masksToBounds = YES;
    self.sexTF.clearsOnBeginEditing = YES;
    self.sexTF.delegate = self;
    
    UILabel *hobby = [[UILabel alloc] initWithFrame:CGRectMake(150, 150, 50, 30)];
    hobby.text = @"爱 好:";
    hobby.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:hobby];
    [hobby release];
    
    self.hobbyTF = [[UITextField alloc] initWithFrame:CGRectMake(210, 150, 150, 30)];
    [self.view addSubview:self.hobbyTF];
    [self.hobbyTF release];
    self.hobbyTF.text = self.dic[@"hobby"];
    self.hobbyTF.backgroundColor = [UIColor grayColor];
    self.hobbyTF.layer.borderWidth = 1;
    self.hobbyTF.layer.cornerRadius = 10;
    self.hobbyTF.layer.masksToBounds = YES;
    self.hobbyTF.clearsOnBeginEditing = YES;
    self.hobbyTF.delegate = self;
    
    UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(150, 200, 50, 30)];
    phone.text = @"电 话:";
    phone.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:phone];
    [phone release];
    
    self.phoneTF = [[UITextField alloc] initWithFrame:CGRectMake(210, 200, 150, 30)];
    [self.view addSubview:self.phoneTF];
    self.phoneTF.text = self.dic[@"phone"];
    self.phoneTF.backgroundColor = [UIColor grayColor];
    self.phoneTF.layer.borderWidth = 1;
    self.phoneTF.layer.cornerRadius = 10;
    self.phoneTF.layer.masksToBounds = YES;
    self.phoneTF.clearsOnBeginEditing = YES;
    self.phoneTF.delegate = self;
    [self.phoneTF release];

    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.button setTitle:@"确定" forState:UIControlStateNormal];
    self.button.frame = CGRectMake(200, 250, 50, 30);
    [self.view addSubview:self.button];
    [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)click:(UIButton *)but
{
    /// 3.设置触发方法
    [self.delegate addName:self.nameTF.text Sex:self.sexTF.text Hobby:self.hobbyTF.text Phone:self.phoneTF.text Image:self.photo];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nameTF resignFirstResponder];
    [self.sexTF resignFirstResponder];
    [self.hobbyTF resignFirstResponder];
    [self.phoneTF resignFirstResponder];
}
- (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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值