03通讯录(代理解耦)

 

//
//  XMGAddViewController.h
//  小码哥通讯录
#import <UIKit/UIKit.h>

@class XMGAddViewController,XMGContact;
@protocol XMGAddViewControllerDelegate <NSObject>

@optional
- (void)addViewController:(XMGAddViewController *)addVc didClickAddBtnWithContact:(XMGContact *)contact;

@end

@class XMGContactViewController;

@interface XMGAddViewController : UIViewController

@property (nonatomic, weak) id<XMGAddViewControllerDelegate> delegate;



@end
//
//  XMGAddViewController.m
//  小码哥通讯录

#import "XMGAddViewController.h"
#import "XMGContact.h"


@interface XMGAddViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;

@end

@implementation XMGAddViewController

// 点击添加的时候调用
- (IBAction)add:(id)sender {
    // 0.把文本框的值包装成联系人模型
    XMGContact *c = [XMGContact contactWithName:_nameField.text phone:_phoneField.text];
    
    // 1.通知代理做事情
    // _delegate = _contactVc
    if ([_delegate respondsToSelector:@selector(addViewController:didClickAddBtnWithContact:)]) {
        [_delegate addViewController:self didClickAddBtnWithContact:c];
    }
    
    
    // 2.回到联系人控制器
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 给文本框添加监听器,及时监听文本框内容的改变
    [_nameField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    [_phoneField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 主动弹出姓名文本框
    [_nameField becomeFirstResponder];
}

// 任一一个文本框的内容改变都会调用
- (void)textChange
{
    _addBtn.enabled = _nameField.text.length && _phoneField.text.length;
    
}

@end

 

//
//  XMGContactViewController.h
//  小码哥通讯录

#import <UIKit/UIKit.h>
@class XMGContact;
@interface XMGContactViewController : UITableViewController




@end
//
//  XMGContactViewController.m
//  小码哥通讯录
#import "XMGContactViewController.h"
#import "XMGAddViewController.h"
#import "XMGContact.h"
@interface XMGContactViewController ()<UIActionSheetDelegate,XMGAddViewControllerDelegate>

@property (nonatomic, strong) NSMutableArray *contacts;

@end

@implementation XMGContactViewController

- (NSMutableArray *)contacts
{
    if (_contacts == nil) {
        
        _contacts = [NSMutableArray array];
        
    }
    return _contacts;
}

// 跳转之前的时候调用
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    
    // 给添加控制器传递联系人控制器属性
    XMGAddViewController *addVc = segue.destinationViewController;
    
    addVc.delegate = self;
}

- (void)addViewController:(XMGAddViewController *)addVc didClickAddBtnWithContact:(XMGContact *)contact
{
    // 把添加界面的联系人模型传递到联系人界面
    
    // 把联系人模型保存到数组
    [self.contacts addObject:contact];
    
    
    // 刷新表格
    [self.tableView reloadData];
    
}

// 点击注销的时候调用
- (IBAction)logout:(id)sender {
    
    // 弹出actionSheet
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"是否注销?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"注销" otherButtonTitles:nil, nil];
    
    [sheet showInView:self.view];
    
}
#pragma mark - UIActionSheetDelegate
// 点击UIActionSheet控件上的按钮调用
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) { // 点击了注销
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.contacts.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 创建标示符
    static NSString *ID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    
    // 获取模型
    XMGContact *c = self.contacts[indexPath.row];
    
    cell.textLabel.text = c.name;
    cell.detailTextLabel.text = c.phone;
    
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#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

 取消分割线

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    // 取消分割线
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // tableView有数据的时候才需要分割线
    
    // 开发小技巧:快速取消分割线
    self.tableView.tableFooterView = [[UIView alloc] init];
    
}

 

posted @ 2017-03-30 23:08  laugh  阅读(225)  评论(0编辑  收藏  举报