首页 > 代码库 > 03通讯录(搭建编辑界面)

03通讯录(搭建编辑界面)

 

////  XMGContactViewController.h//  小码哥通讯录#import <UIKit/UIKit.h>@class XMGContact;@interface XMGContactViewController : UITableViewController@end
////  XMGContactViewController.m//  小码哥通讯录//#import "XMGContactViewController.h"#import "XMGAddViewController.h"#import "XMGEditViewController.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;    }#pragma mark - 添加控制器代理方法- (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];    //    // 取消分割线//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;        // tableView有数据的时候才需要分割线        // 开发小技巧:快速取消分割线    self.tableView.tableFooterView = [[UIView alloc] init];    }#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;}#pragma mark - tableView代理方法// 点击cell的时候调用- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // 加载storyboard    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];            // 创建编辑控制器     XMGEditViewController *editVc = [storyboard instantiateViewControllerWithIdentifier:@"edit"];            editVc.contact = self.contacts[indexPath.row];        // 跳转到编辑界面    [self.navigationController pushViewController:editVc animated:YES];    }@end

 

////  XMGEditViewController.h//  小码哥通讯录#import <UIKit/UIKit.h>@class XMGContact;@interface XMGEditViewController : UIViewController@property (nonatomic, strong) XMGContact *contact;@end
////  XMGEditViewController.m//  小码哥通讯录#import "XMGEditViewController.h"#import "XMGContact.h"@interface XMGEditViewController ()@property (weak, nonatomic) IBOutlet UITextField *nameField;@property (weak, nonatomic) IBOutlet UITextField *phoneField;@property (weak, nonatomic) IBOutlet UIButton *saveBtn;@end@implementation XMGEditViewController/* 控制器之间传值:一定要注意控制器的子控件有没有加载,一定要在子控件加载完成的时候才去给子控件赋值,一般在viewDidLoad给控件赋值。  */- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.        // 设置导航条的标题    self.title = @"查看/编辑界面";        // 设置导航条右边的按钮    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(edit:)];            // 给文本框    _nameField.text = _contact.name;    _phoneField.text = _contact.phone;    // 给文本框添加监听器,及时监听文本框内容的改变    [_nameField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];    [_phoneField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];            // 判断下登录按钮能否点击    [self textChange];    }// 任一一个文本框的内容改变都会调用- (void)textChange{    _saveBtn.enabled = _nameField.text.length && _phoneField.text.length;}// 点击编辑的时候调用- (void)edit:(UIBarButtonItem *)item{    NSLog(@"%@",item);        if ([item.title isEqualToString:@"编辑"]) {        // 更改标题        item.title = @"取消";                // 让文本框允许编辑        _nameField.enabled = YES;        _phoneField.enabled = YES;                // 弹出电话文本框的键盘        [_phoneField becomeFirstResponder];                // 显示保存按钮        _saveBtn.hidden = NO;                   }else{        // 更改标题        item.title = @"编辑";        //        // 退出键盘//        [self.view endEditing:YES];                // 隐藏保存按钮        _saveBtn.hidden = YES;                // 让文本框不允许编辑        _nameField.enabled = NO;        _phoneField.enabled = NO;    }   }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

小码哥通讯录(保存功能)

////  XMGEditViewController.h//  小码哥通讯录#import <UIKit/UIKit.h>@class XMGContact;// 定义block类型别名typedef void(^XMGEditViewControllerBlock)();@interface XMGEditViewController : UIViewController@property (nonatomic, strong) XMGContact *contact;@property (nonatomic, strong) XMGEditViewControllerBlock block;@end
////  XMGEditViewController.m//  小码哥通讯录#import "XMGEditViewController.h"#import "XMGContact.h"@interface XMGEditViewController ()@property (weak, nonatomic) IBOutlet UITextField *nameField;@property (weak, nonatomic) IBOutlet UITextField *phoneField;@property (weak, nonatomic) IBOutlet UIButton *saveBtn;@end@implementation XMGEditViewController// 点击保存的时候调用- (IBAction)save:(id)sender {    // 修改模型数据    _contact.name = _nameField.text;    _contact.phone = _phoneField.text;    #warning TODO:    // 让小弟做事情,刷新表格    if (_block) {                _block();    }            // 回到上一个控制器    [self.navigationController popViewControllerAnimated:YES];    }/* 控制器之间传值:一定要注意控制器的子控件有没有加载,一定要在子控件加载完成的时候才去给子控件赋值,一般在viewDidLoad给控件赋值。  */- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.        // 设置导航条的标题    self.title = @"查看/编辑界面";        // 设置导航条右边的按钮    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(edit:)];            // 给文本框    _nameField.text = _contact.name;    _phoneField.text = _contact.phone;    // 给文本框添加监听器,及时监听文本框内容的改变    [_nameField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];    [_phoneField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];            // 判断下登录按钮能否点击    [self textChange];    }// 任一一个文本框的内容改变都会调用- (void)textChange{    _saveBtn.enabled = _nameField.text.length && _phoneField.text.length;}// 点击编辑的时候调用- (void)edit:(UIBarButtonItem *)item{    NSLog(@"%@",item);        if ([item.title isEqualToString:@"编辑"]) {        // 更改标题        item.title = @"取消";                // 让文本框允许编辑        _nameField.enabled = YES;        _phoneField.enabled = YES;                // 弹出电话文本框的键盘        [_phoneField becomeFirstResponder];                // 显示保存按钮        _saveBtn.hidden = NO;                   }else{        // 更改标题        item.title = @"编辑";        //        // 退出键盘//        [self.view endEditing:YES];                // 隐藏保存按钮        _saveBtn.hidden = YES;                // 让文本框不允许编辑        _nameField.enabled = NO;        _phoneField.enabled = NO;                // 还原数据        _nameField.text = _contact.name;        _phoneField.text = _contact.phone;    }   }- (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
////  XMGContactViewController.m//  小码哥通讯录#import "XMGContactViewController.h"#import "XMGAddViewController.h"#import "XMGEditViewController.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;    }#pragma mark - 添加控制器代理方法- (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];    //    // 取消分割线//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;        // tableView有数据的时候才需要分割线        // 开发小技巧:快速取消分割线    self.tableView.tableFooterView = [[UIView alloc] init];    }#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;}#pragma mark - tableView代理方法// 点击cell的时候调用- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // 加载storyboard    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];            // 创建编辑控制器     XMGEditViewController *editVc = [storyboard instantiateViewControllerWithIdentifier:@"edit"];            editVc.contact = self.contacts[indexPath.row];    editVc.block = ^(){ // 刷新表格        [self.tableView reloadData];    };        // 跳转到编辑界面    [self.navigationController pushViewController:editVc animated:YES];    }@end

 

03通讯录(搭建编辑界面)