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 1;
}
//每组有多少行(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-1 inSection:0];
    [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://www.cnblogs.com/BJTUzhengli/p/5106604.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在uniapp中创建手机通讯录联系人添加到手机通讯录现有联系人需要借助uniapp提供的API实现。具体步骤如下: 1. 引入uni-app提供的联系人API,例如: ```javascript import contact from '@/common/uni-contact/uni-contact.js' ``` 2. 创建联系人 ```javascript // 创建联系人 function createContact(name, phone, email) { // 构造联系人对象 const contactInfo = { displayName: name, // 显示名称 phoneNumbers: [ { label: '手机', value: phone } ], emails: [ { label: '邮箱', value: email } ] } // 调用API创建联系人 contact.add(contactInfo, (res) => { console.log(res) if (res.code === 0) { console.log('创建联系人成功') } else { console.log('创建联系人失败') } }) } ``` 在以上代码中,我们首先构造了一个联系人对象,包含了联系人的姓名、电话和邮箱信息。然后,我们调用了uni-app提供的联系人API中的add()方法,将联系人对象传递给该方法进行创建。在回调函数中,我们可以根据返回的状态码来判断创建联系人是否成功。 3. 添加到现有联系人 ```javascript // 添加到现有联系人 function addToExistingContact(name, phone, email) { // 构造联系人对象 const contactInfo = { displayName: name, // 显示名称 phoneNumbers: [ { label: '手机', value: phone } ], emails: [ { label: '邮箱', value: email } ] } // 调用API添加到现有联系人 contact.chooseContact((res) => { console.log(res) if (res.code === 0) { const contactId = res.contactId contact.update(contactId, contactInfo, (res) => { console.log(res) if (res.code === 0) { console.log('添加到现有联系人成功') } else { console.log('添加到现有联系人失败') } }) } else { console.log('选择联系人失败') } }) } ``` 在以上代码中,我们首先构造了一个联系人对象,包含了联系人的姓名、电话和邮箱信息。然后,我们调用了uni-app提供的联系人API中的chooseContact()方法,让用户选择要添加联系人的现有联系人。在选择联系人后,我们将联系人对象和联系人ID传递给update()方法进行更新。在回调函数中,我们可以根据返回的状态码来判断添加联系人是否成功。 注意:以上代码中的contact.add()和contact.update()方法仅在uni-app中可用,不能在微信小程序中使用。在微信小程序中创建和修改联系人需要使用微信提供的接口。此外,上述代码中的后端接口需要自己实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值