一:创建模型对象:contact用于存放数据,也便于读取加载

#import <Foundation/Foundation.h>
@interface contact : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *tel;
@end

二:在addContackViewController中设置代理协议,并监听输入栏

#import <UIKit/UIKit.h>

@class addContactViewConroller,contact;

@protocol AddContactViewConrollerDelegate<NSObject>
@optional
- (void)addContactViewController:(addContactViewConroller *)addContactViewController didSaveContactwithName:(NSString*)name tel:(NSString *)tel;
//传递模型使得代码可维护性更高
- (void) addContactViewController:(addContactViewConroller *)addContactViewController didSaveContactWithContact:(contact *)contact;
@end @interface addContactViewConroller :UIViewController
@property (weak, nonatomic) id<AddContactViewConrollerDelegate>delegate;
@end

在.m文件中代码如下:

#import "addContactViewConroller.h"
#import "contact.h"
@interface addContactViewConroller ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *telField;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
@end
@implementation addContactViewConroller - (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)saveBtnClick {
// 判断代理是否实现了代理方法
// if ([self.delegate respondsToSelector:@selector(addContactViewController:didSaveContactwithName:tel:)]) {
// [self.delegate addContactViewController:self didSaveContactwithName:self.nameField.text tel:self.telField.text];
if ([self.delegate respondsToSelector:@selector(addContactViewController:didSaveContactWithContact:)]) {
contact *con = [[contact alloc] init];
con.name = self.nameField.text;
con.tel = self.telField.text;
// 直接专递给代理模型数据
[self.delegate addContactViewController:self didSaveContactWithContact:con];
}
}
@end

三:

1)在contactTableViewController中创建数组用于保存接受的到模型数据,代码如下:

#import "contactTableViewController.h"
#import "contact.h"
#import "addContactViewConroller.h" @interface contactTableViewController ()<AddContactViewConrollerDelegate>
//定义一个可变数组,用于存放联系人
@property (strong, nonatomic) NSMutableArray *contacts; @end

2)实现该控制器的数据源方法,并从模型中加载数据显示到cell上

代码如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ;
}
//每组有多少行(row)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.contacts.count;
}
//设置显示内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s",__func__);
// 需要先设置cell的id,用于创建可重用cell。
//1.获得可重用的id
static NSString *Id = @"contactCell";
//2.创建可重用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Id];
//3.显示数据
contact *contact = self.contacts[indexPath.row];
cell.textLabel.text = contact.name;
cell.detailTextLabel.text = contact.tel;
//4.返回cell
return cell;
}

3)设置自己成为addContactViewController的代理并实现代理方法,代码如下:

//- (void)addContactViewController:(addContactViewConroller *)addContactViewController didSaveContactwithName:(NSString *)name tel:(NSString *)tel
//{
//// 建立模型并赋值
// contact *con = [[contact alloc] init];
// con.name = name;
// con.tel = tel;
//// 把模型放进数组里
// [self.contacts addObject:con];
//// 隐藏目标控制器
//#warning 虽然是代理调用的这个方法,但是隐藏的还是目标控制器!
// [self.navigationController popViewControllerAnimated:YES];
//// 刷新tableView
// [self.tableView reloadData];
//}
- (void) addContactViewController:(addContactViewConroller *)addContactViewController didSaveContactWithContact:(contact *)contact
{
// 将模型放进数组里 [self.contacts addObject:contact]; // 刷新tableView
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.contacts.count- inSection:];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
// [self.tableView reloadData]; // 隐藏目标控制器
[self.navigationController popViewControllerAnimated:YES];
}

4)从目标控制器中获得数据,代码如下:

//获取目标控制器
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
id destVc =segue.destinationViewController;
// 判断目标控制器的类型
if ([destVc isKindOfClass:[addContactViewConroller class]]) {
addContactViewConroller *addContactViewController = destVc;
// 如果符合目标控制器的类型,则设置目标控制的代理
addContactViewController.delegate = self;
}
}

四:实际效果如下:

2016-1-6第一个完整APP 私人通讯录的实现 3:添加联系人的更多相关文章

  1. 2016-1-5第一个完整APP 私人通讯录的实现 1:登录界面及跳转的简单实现2

    ---恢复内容开始--- 实际效果如上 一:Segue的学习 1.什么是Segue: Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue对象(简称Segue)   ...

  2. 2016-1-7第一个完整APP 私人通讯录的实现 6:在联系人界面增加删除联系人的功能

    一:在viewDidLoad方法中代码添加一个UIBarButtonItem,并将其的类型设置成垃圾桶,代码如下: - (void)viewDidLoad { [super viewDidLoad]; ...

  3. 2016-1-7第一个完整APP 私人通讯录的实现 5:保存数据

    一:登陆界面 1):用户点击登陆按钮并成功登陆后,根据此时的开关情况选择是否保存数据,代码如下: "]) { [self performSegueWithIdentifier:@" ...

  4. 2016-1-6第一个完整APP 私人通讯录的实现 4:编辑联系人

    一:建立编辑联系人的controller,并使其拥有模型contact,且有协议.代码如下 #import <UIKit/UIKit.h> #import "contact.h& ...

  5. 2016-1-6第一个完整APP 私人通讯录的实现 2:增加提示用户的提示框,监听文本框

    一:在登录时弹出提示用户的提示框: 1.使用第三方框架. 2.在登陆按钮点击事件中增加如下代码: - (IBAction)loginBtnClicked { NSString *acount = se ...

  6. iOS完整App资源收集

    前言 iOS开发学习者都希望得到实战训练,但是很多资料都是只有一小部分代码,并不能形成完成的App,笔者在此处收集了很多开源的完整的App,都有源代码哦! 本篇文章持续更新中,请持续关注.本篇所收集的 ...

  7. MUI框架开发HTML5手机APP(一)--搭建第一个手机APP

      前  言 JRedu 随着HTML5的不断发展,移动开发成为主流趋势!越来越多的公司开始选择使用HTML5开发手机APP,而随着手机硬件设备配置的不断提升,各种开发框架的不断优化,也使着H5开发的 ...

  8. MUI框架开发HTML5手机APP(一)--搭建第一个手机APP(转)

    出处:http://www.cnblogs.com/jerehedu/p/7832808.html  前  言 JRedu 随着HTML5的不断发展,移动开发成为主流趋势!越来越多的公司开始选择使用H ...

  9. 浅谈MAIC 2016第二届移动应用(APP)创新大会

    MAIC 2016第二届移动应用(APP)创新大会将于2016年12月在上海举办!MAIC一届比一届办的有质量,规模越大.今年也如约而至,预计今年MAIC规模逾2000人.大会以专业会议,创新应用展览 ...

随机推荐

  1. python常见的模块

    Python内置模块名称 功能简介 详细解释/使用示例 os 和操作系统相关 os.path — Common pathname manipulations sys 和系统相关 sys — Syste ...

  2. jquery 获取属性的值

    jquery中用attr()方法来获取和设置元素属性,attr是attribute(属性)的缩写,在jQuery DOM操作中会经常用到attr(),attr()有4个表达式. 1.  attr( 属 ...

  3. (12)odoo各种提前期和时间

    1)Product的提前期    Customer Lead Time(sale_delay):客户提前期,指SO确认到向客户发货的天数,由于销售数量不同该时间也不同,因此,这里是一个平均时间.    ...

  4. poj---(2886)Who Gets the Most Candies?(线段树+数论)

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10373   Acc ...

  5. Uva---10881 Piotr's Ants(蚂蚁)

    Problem DPiotr's AntsTime Limit: 2 seconds "One thing is for certain: there is no stopping them ...

  6. 用HTML5实现的各种排序算法的动画比较 及算法小结

    用HTML5实现的各种排序算法的动画比较 http://www.webhek.com/misc/comparison-sort/ 几种排序算法效率的比较 来源:http://blog.chinauni ...

  7. JFrame背景

    1.引言 在了解了JFrame面板的相关知识后,我们可以选择在RootPane根面板或LayeredPane面板中设置背景图案. 2.方法 对于大小固定的窗口背景设置如下: //导入图案 ImageI ...

  8. Android 页面滑动

    1.PagerAdapter适配器     PagerAdapter主要是viewpager的适配器,而viewPager是android.support.v4扩展中新添加的一个强大控件,可以实现控件 ...

  9. Linux学习之CentOS--CentOS6.4下Mysql数据库的安装与配置【转】

      如果要在Linux上做j2ee开发,首先得搭建好j2ee的开发环境,包括了jdk.tomcat.eclipse的安装(这个在之前的一篇随笔中已经有详细讲解了Linux学习之CentOS(七)--C ...

  10. Linux系统中的日常监控知识点

    1.命令熟悉之w [xiongchao@oc3006745124 Desktop]$ w :: up :, users, load average: 1.48, 1.19, 1.11 USER TTY ...