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

2016-1-6第一个完整APP 私人通讯录的实现 3:添加联系人,第1张

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;
}
}

四:实际效果如下:

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/587686.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存