联系人数据存储Demo源代码
- 源码下载地址:
07-联系人数据存储.zip
35.8 KB 
// MJPerson.h
- //
- // MJPerson.h
- // 07-联系人数据存储
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- ////本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490609.html
- #import<Foundation/Foundation.h>
- @interface MJPerson : NSObject <NSCoding>
- @property (nonatomic,copy) NSString *name;
- @property (nonatomic,copy) NSString *phone;
- @end
// MJPerson.m
- //
- // MJPerson.m
- // 07-联系人数据存储
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJPerson.h"
- @implementation MJPerson
- - (void)encodeWithCoder:(NSCoder *)encoder
- {
- [encoder encodeObject:_name forKey:@"name"];
- [encoder encodeObject:_phone forKey:@"phone"];
- }
- - (id)initWithCoder:(NSCoder *)decoder
- {
- if(self= [super init]) {
- _name = [decoder decodeObjectForKey:@"name"];
- _phone = [decoder decodeObjectForKey:@"phone"];
- }
- return self;
- }
- @end
// MJFriendsViewController.h
- //
- // MJFriendsViewController.h
- // 07-联系人数据存储
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<UIKit/UIKit.h>
- @interface MJFriendsViewController : UITableViewController
- @end
// MJFriendsViewController.m
- //
- // MJFriendsViewController.m
- // 07-联系人数据存储
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- ////本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490609.html
- #define kFilePath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"persons.data"]
- #import "MJFriendsViewController.h"
- #import "MJAddViewController.h"
- #import "MJPerson.h"
- @interface MJFriendsViewController () <MJAddViewControllerDelegate>
- {
- NSMutableArray *_persons;
- }
- @end
- @implementation MJFriendsViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
- // NSString *path = [doc stringByAppendingPathComponent:@"persons.data"];
- // _persons = [PersonTool persons];
- _persons = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath];
- if(_persons ==nil) {
- _persons = [NSMutableArray array];
- }
- }
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
- {
- // 1.导航控制器
- UINavigationController *nav = segue.destinationViewController;
- // 2.取出栈顶的添加控制器
- MJAddViewController *add = (MJAddViewController *)nav.topViewController;
- // 3.设置代理
- add.delegate =self;
- }
- #pragma mark - MJAddViewController的代理方法
- #pragma mark成功添加一个联系人就会调用
- - (void)addViewController:(MJAddViewController *)add didAddPerson:(MJPerson *)person
- {
- // 1.将人塞进数组中
- [_persons insertObject:person atIndex:0];
- // 2.刷新表格
- [self.tableView reloadData];
- // 3.归档
- // NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
- ////本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490609.html
- // NSString *path = [doc stringByAppendingPathComponent:@"persons.data"];
- [NSKeyedArchiver archiveRootObject:_persons toFile:kFilePath];
- // [PersonTool savePersons:_persons];
- }
- #pragma mark - Table view data source
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return _persons.count;
- }
- #pragma mark每一行显示怎样的cell(内容)
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // 1.定义一个标识
- static NSString *ID =@"cell";
- // 2.去缓存池中取出可循环利用的cell
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
- // 3.如果缓存中没有可循环利用的cell
- if(cell ==nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
- }
- // 4.取出模型设置数据
- MJPerson *p = _persons[indexPath.row];
- cell.textLabel.text = p.name;
- cell.detailTextLabel.text = p.phone;
- return cell;
- }
- @end
// MJAddViewController.h
- //
- // MJAddViewController.h
- // 07-联系人数据存储
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<UIKit/UIKit.h>
- @classMJAddViewController, MJPerson;
- @protocolMJAddViewControllerDelegate <NSObject>
- @optional
- - (void)addViewController:(MJAddViewController *)add didAddPerson:(MJPerson *)person;
- @end
- @interface MJAddViewController : UIViewController
- - (IBAction)cancel:(id)sender;
- - (IBAction)add:(id)sender;
- @property(weak,nonatomic)IBOutletUITextField *nameField;
- @property(weak,nonatomic)IBOutletUITextField *phoneField;
- @property(nonatomic,weak)id<MJAddViewControllerDelegate> delegate;
- @end
// MJAddViewController.m
- //
- // MJAddViewController.m
- // 07-联系人数据存储
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJAddViewController.h"
- #import"MJPerson.h"
- @interfaceMJAddViewController ()
- @end
- @implementationMJAddViewController
- - (void)viewDidLoad
- {
- [superviewDidLoad];
- // Do any additional setup after loading the view.
- }
- - (IBAction)cancel:(id)sender {
- [self dismissViewController Animated:YES completion:nil];
- }
- - (IBAction)add:(id)sender {
- if([_delegate respondsToSelector:@selector(addViewController:didAddPerson:)]) {
- MJPerson *p = [[MJPerson alloc] init];
- p.name = _nameField.text;
- p.phone = _phoneField.text;
- [_delegate addViewController:self didAddPerson:p];
- [self dismissViewControllerAnimated:YEScompletion:nil];
- }
- }
- @end
联系人数据存储Demo源代码的更多相关文章
- 七牛云数据存储Demo
利用七牛云的Python SDK实现文件上传.下载等操作. import os import requests import qiniu from qiniu import BucketManager ...
- 1211笔记关于//modal//更改窗口的根控制器//数据存取//Plist属性列表//-“沙盒机制”//plis属性列表//偏好设置//归档普通对象//联系人数据存储//协议与回调函数
一.利用Modal形式展示控制器 1.如何展示// vc就是要展示的新控制器[self presentViewController:vc animated:YES completion:^{ N ...
- Web自动化框架之五一套完整demo的点点滴滴(excel功能案例参数化+业务功能分层设计+mysql数据存储封装+截图+日志+测试报告+对接缺陷管理系统+自动编译部署环境+自动验证false、error案例)
标题很大,想说的很多,不知道从那开始~~直接步入正题吧 个人也是由于公司的人员的现状和项目的特殊情况,今年年中后开始折腾web自动化这块:整这个原因很简单,就是想能让自己偷点懒.也让减轻一点同事的苦力 ...
- 看懂Qt源代码-Qt源码的对象数据存储
第一次看Qt源代码的人都会被其代码所迷惑,经常会看到代码中的d_ptr成员.d_func(函数)和Q_DECLARE_PRIVATE等奇怪的宏,总是让人一头雾水,下面这篇文章转自http://www. ...
- android 数据存储Ⅱ
本章继续讲解在Android开发中,数据的存储与管理.涉及知识点:SQLite,SwipeRefreshLayout控件刷新. 1.功能需求 练习使用SQLite 做一个登录界面,数据库字段包含用户名 ...
- Android中数据存储(一)
国庆没有给国家添堵,没有勾搭妹子,乖乖的写着自己的博客..... 本文将为大家介绍Android中数据存储的五种方式,数据存储可是非常重要的知识哦. 一,文件存储数据 ①在ROM存储数据 关于在ROM ...
- ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello ...
- Android系统的五种数据存储形式(二)
之前介绍了Android系统下三种数据存储形式,今天补充介绍另外两种,分别是内容提供者和网络存储.有些人可能认为内存提供者和网络存储更偏向于对数据的操作而不是数据的存储,但这两种方式确实与数据有关,所 ...
- Android 数据存储五种方式
1.概述 Android提供了5种方式来让用户保存持久化应用程序数据.根据自己的需求来做选择,比如数据是否是应用程序私有的,是否能被其他程序访问,需要多少数据存储空间等,分别是: ① 使用Shared ...
随机推荐
- 让Dreamweaver支持cshtml (MVC Razor环境)
介绍:让Dreamweaver支持cshtml 正文: 如题,刚才搜了很久,都搜不到答案,幸好得到“包大人”(同事)的帮助,才得以解决. DW支持很多文件类型的代码提示,可是类型太多,不可能全部都有, ...
- 算法搬运之BFPRT算法
原文连接:http://noalgo.info/466.html BFPRT算法,又称为中位数的中位数算法,由5位大牛(Blum . Floyd . Pratt . Rivest . Tarjan)提 ...
- Python网络编程(进程池、进程间的通信)
线程池的原理: 线程池是预先创建线程的一种技术.线程池在还没有任务到来之前, 创建一定数量的线程,放入空闲队列中.这些线程都是处于睡眠状态, 即均为启动,不消 ...
- LAXCUS对数据存储的优化
LAXCUS兼容行存储(NSM)和列存储(DSM)两种数据模型,实现了混合存储.同时在分布环境里,做到将数据的分发和备份自动处理,这样就不再需要人工干预了. 行存储,为了兼容广大用户对 ...
- Web负载均衡技术
Web负载均衡(Load Balancing),简单地说就是给我们的服务器集群分配“工作任务”,而采用恰当的分配方式,对于保护处于后端的Web服务器来说,非常重要. 负载均衡的策略有很多,我们从简单的 ...
- 论文翻译 - Multiagent Bidirectionally-Coordinated Nets Emergence of Human-level Coordination in Learning to Play StarCraft Combat Games
(缺少一些公式的图或者效果图,评论区有惊喜) (个人学习这篇论文时进行的翻译[谷歌翻译,你懂的],如有侵权等,请告知) Multiagent Bidirectionally-Coordinated N ...
- Ubuntu 和 Windows 之间进行远程访问和文件互传
1. 利用 Ubuntu 自带软件 Remmina 对另一台 Ubuntu 电脑进行远程访问(同一局域网下) 假设要用 A 电脑来控制 B 电脑,首先需要在 B 电脑上进行桌面共享设置 . 然后打 ...
- BZOJ 4031 HEOI2015 小Z的房间 基尔霍夫矩阵+行列式+高斯消元 (附带行列式小结)
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可 ...
- SpringMVC 上传图片保存到服务器 同时更改图片名称保存至数据库
@RequestMapping(value = "/save.do", method = RequestMethod.POST) public String saveDriv ...
- C++STL中的vector的简单实用
[原创] 使用C++STL中的vector, #include <stdio.h> #include<stdlib.h> #include<vector> usin ...