2016-1-6第一个完整APP 私人通讯录的实现 3:添加联系人
一:创建模型对象: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:添加联系人的更多相关文章
- 2016-1-5第一个完整APP 私人通讯录的实现 1:登录界面及跳转的简单实现2
---恢复内容开始--- 实际效果如上 一:Segue的学习 1.什么是Segue: Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue对象(简称Segue) ...
- 2016-1-7第一个完整APP 私人通讯录的实现 6:在联系人界面增加删除联系人的功能
一:在viewDidLoad方法中代码添加一个UIBarButtonItem,并将其的类型设置成垃圾桶,代码如下: - (void)viewDidLoad { [super viewDidLoad]; ...
- 2016-1-7第一个完整APP 私人通讯录的实现 5:保存数据
一:登陆界面 1):用户点击登陆按钮并成功登陆后,根据此时的开关情况选择是否保存数据,代码如下: "]) { [self performSegueWithIdentifier:@" ...
- 2016-1-6第一个完整APP 私人通讯录的实现 4:编辑联系人
一:建立编辑联系人的controller,并使其拥有模型contact,且有协议.代码如下 #import <UIKit/UIKit.h> #import "contact.h& ...
- 2016-1-6第一个完整APP 私人通讯录的实现 2:增加提示用户的提示框,监听文本框
一:在登录时弹出提示用户的提示框: 1.使用第三方框架. 2.在登陆按钮点击事件中增加如下代码: - (IBAction)loginBtnClicked { NSString *acount = se ...
- iOS完整App资源收集
前言 iOS开发学习者都希望得到实战训练,但是很多资料都是只有一小部分代码,并不能形成完成的App,笔者在此处收集了很多开源的完整的App,都有源代码哦! 本篇文章持续更新中,请持续关注.本篇所收集的 ...
- MUI框架开发HTML5手机APP(一)--搭建第一个手机APP
前 言 JRedu 随着HTML5的不断发展,移动开发成为主流趋势!越来越多的公司开始选择使用HTML5开发手机APP,而随着手机硬件设备配置的不断提升,各种开发框架的不断优化,也使着H5开发的 ...
- MUI框架开发HTML5手机APP(一)--搭建第一个手机APP(转)
出处:http://www.cnblogs.com/jerehedu/p/7832808.html 前 言 JRedu 随着HTML5的不断发展,移动开发成为主流趋势!越来越多的公司开始选择使用H ...
- 浅谈MAIC 2016第二届移动应用(APP)创新大会
MAIC 2016第二届移动应用(APP)创新大会将于2016年12月在上海举办!MAIC一届比一届办的有质量,规模越大.今年也如约而至,预计今年MAIC规模逾2000人.大会以专业会议,创新应用展览 ...
随机推荐
- [redis] redis 存取键值对常用的三种使用方式 - Jedis、JedisPool、Jedis分布式
|-Jedis 普通方式 |-JedisPool 连接池方式(需要引入pool相关jar) |-Jedis 分布式 (需要引入pool相关jar) 引入jedis2.7.0和commons.pool2 ...
- --专访雷果国: 从1.5K到18K 一个程序员的5年成长之路--
导语:今年三月份,在CSDN博客和新浪微博上有一篇<从1.5K到18K,一个程序员的5年成长之路>被众人分享和传阅,这篇博文首先介绍了作者自学之初薄弱的基础,然后通过流水账形式分享了那个从 ...
- hdu--(1247)Hat’s Words(trie树)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Anagrams [LeetCode]
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- 转:Nginx+Apache环境的安装与配置
转:http://www.server110.com/nginx/201404/8817.html 我们依然尽可能采用yum来安装我们需要的软件,由系统官方维护的软件,其安全性和稳定性都值得信赖,并且 ...
- [转]BeginInvoke和EndInvoke方法浅析
开发语言:C#3.0 IDE:Visual Studio 2008 一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提 ...
- linux 查看僵尸进程
top -b -i -n 1 查看僵死进程命令 ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' 查看apache 当前进程数 ps -ef | grep ht ...
- Spring MVC Controller中解析GET方式的中文参数会乱码的问题(tomcat如何解码)
Spring MVC Controller中解析GET方式的中文参数会乱码的问题 问题描述 在工作上使用突然出现从get获取中文参数乱码(新装机器,tomcat重新下载和配置),查了半天终于找到解决办 ...
- JavaWeb chapter 1 http协议
1. 静态web和动态web的区别: 静态web和动态web最本质的区别是静态web是无法进行数据库操作,而动态web是可以进行数据库操作的.动态web的最大特点就是具有交互性,所谓交互性就是服务器 ...
- BroadcastReceiver的实例----基于Service的音乐播放器之二
该程序的后台Service会在播放状态发生改变时对外发送广播(广播将会激发前台Activity的BroadcastReceiver):它也会采用BroadcastReceiver监听来自前台Activ ...