ContactViewController
//
// MJContactsViewController.m
// 01-私人通讯录
//
// Created by apple on 14-4-10.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "MJContactsViewController.h"
#import "MJAddViewController.h"
#import "MJEditViewController.h"
#import "MJContact.h"
#import "MJContactCell.h"
@interface MJContactsViewController () <UIActionSheetDelegate, MJAddViewControllerDelegate, MJEditViewControllerDelegate>
- (IBAction)logout:(id)sender;
@property (nonatomic, strong) NSMutableArray *contacts;
@end
@implementation MJContactsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// self.tableView.rowHeight = 70;
}
- (NSMutableArray *)contacts
{
if (_contacts == nil) {
_contacts = [NSMutableArray array];
}
return _contacts;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.contacts.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
MJContactCell *cell = [MJContactCell cellWithTableView:tableView];
// 2.设置cell的数据
cell.contact = self.contacts[indexPath.row];
return cell;
}
//- (void)setName:(NSString *)name phone:(NSString *)phone
//{
// NSLog(@"MJContactsViewController-%@-%@", name, phone);
//}
/**
* 注销
*/
- (IBAction)logout:(id)sender {
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定要注销?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
[sheet showInView:self.view];
}
#pragma mark - actionsheet的代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != 0) return;
[self.navigationController popViewControllerAnimated:YES];
}
/**
* 执行跳转之前会调用
* 在这个方法中,目标控制器的view还没有被创建,
所以对于<查看联系人> 页面的数据添加不应该放在setContact中,而是应该放在ViewDidLoad中设置
// 数据的正向传递:将被点击的cell的数据从ContactList --》 Edit
ContactsList -- 》 Edit
在 ContactViewController向EditViewControllert跳转的方法中(比如EditBtnClick)将数据传递给EditViewController
//数据的正向传递
顺传数据的时候不要重写set方法,要在ViewDidLoad方法中取得数据,来赋值给界面上的UI控件。
因为set方法使用的时候,EditView还没有创建出来。
因为控制器View 是延迟加载的,什么时候用到什么时候创建,在传递模型数据的那一刻没有做跳转的动作(我们所进行的操作都是在页面跳转之前进行的)
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
id vc = segue.destinationViewController;
if ([vc isKindOfClass:[MJAddViewController class]]) { // 如果是跳转到添加联系人的控制器
// 设置下一个控制器(添加联系人的控制器)的代理
MJAddViewController *addVc = vc;
addVc.delegate = self;
} else if ([vc isKindOfClass:[MJEditViewController class]]) { // 如果是跳转到查看(编辑)联系人的控制器
MJEditViewController *editVc = vc;
// 取得选中的那行
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
editVc.contact = self.contacts[path.row];//在EditViewController定义一个public模型属性,我们在View创建之前将模型数据传递给Edit,但是在ViewDidLoad中进行数据的赋值
editVc.delegate = self;
}
}
#pragma mark - MJAddViewController的代理方法
- (void)addViewController:(MJAddViewController *)addVc didAddContact:(MJContact *)contact
{
// 1.添加模型数据
[self.contacts addObject:contact];
// 2.刷新表格
[self.tableView reloadData];
}
#pragma mark - MJEditViewController的代理方法
- (void)editViewController:(MJEditViewController *)editVc didSaveContact:(MJContact *)contact
{
[self.tableView reloadData];
}
@end
EditViewController.h
//
// MJEditViewController.h
#import <UIKit/UIKit.h>
@class MJContact, MJEditViewController;
@protocol MJEditViewControllerDelegate <NSObject>
@optional
- (void)editViewController:(MJEditViewController *)editVc didSaveContact:(MJContact *)contact;
@end
@interface MJEditViewController : UIViewController
@property (nonatomic, strong) MJContact *contact;
@property (nonatomic, weak) id<MJEditViewControllerDelegate> delegate;
@end
EditViewController.m
//
// MJEditViewController.m
#import "MJEditViewController.h"
#import "MJContact.h"
@interface MJEditViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
- (IBAction)edit:(UIBarButtonItem *)item;
- (IBAction)save;
@end
@implementation MJEditViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 设置数据
self.nameField.text = self.contact.name;
self.phoneField.text = self.contact.phone;
// 监听通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameField];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.phoneField];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
/**
* 文本框的文字发生改变的时候调用
*/
- (void)textChange
{
self.saveBtn.enabled = (self.nameField.text.length && self.phoneField.text.length);
}
- (IBAction)edit:(UIBarButtonItem *)item {
if (self.nameField.enabled) { // 点击的是"取消"
self.nameField.enabled = NO;
self.phoneField.enabled = NO;
[self.view endEditing:YES];
self.saveBtn.hidden = YES;
item.title = @"编辑";
// 还原回原来的数据
self.nameField.text = self.contact.name;
self.phoneField.text = self.contact.phone;
} else { // 点击的是"编辑"
self.nameField.enabled = YES;
self.phoneField.enabled = YES;
[self.phoneField becomeFirstResponder];
self.saveBtn.hidden = NO;
item.title = @"取消";
}
}
/**
* 保存
*/
- (IBAction)save {
// 1.关闭页面
[self.navigationController popViewControllerAnimated:YES];
// 2.通知代理
if ([self.delegate respondsToSelector:@selector(editViewController:didSaveContact:)]) {
// 更新模型数据
self.contact.name = self.nameField.text;
self.contact.phone = self.phoneField.text;
[self.delegate editViewController:self didSaveContact:self.contact];
}
}
@end