联系人数据存储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
Map - //
- // 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
Map - //
- // 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
Map - //
- // 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
Map - //
- // 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
Map - //
- // 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
作者:
出处:http://www.cnblogs.com/ChenYilong/(点击RSS订阅)
本文版权归作者和博客园共有,欢迎转载,
但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。