iOS奋斗ios实用开发技巧iOS学习

iOS通讯录联系人增加和编辑

2017-03-09  本文已影响1190人  凯文Kevin21

  1. 导入#import <AddressBookUI/ABNewPersonViewController.h>框架。
  2. 遵守协议ABNewPersonViewControllerDelegate代理。
  3. 设置rightItem:
 UIBarButtonItem *rightBBItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Addcontacts:)];
    self.navigationItem.rightBarButtonItem = rightBBItem;
  1. 具体联系人增加代码如下:
//iOS9.0以下 
ABNewPersonViewController  *picker = [[ABNewPersonViewController alloc]init];
        picker.newPersonViewDelegate = self;
        
        UINavigationController  *navigation = [[UINavigationController alloc]initWithRootViewController:picker];
        [self presentViewController:navigation animated:YES completion:^{
            //刷新UI
        }];
#pragma mark - ABNewPersonViewControllerDelegate
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(nullable ABRecordRef)person{
    [newPersonView dismissViewControllerAnimated:YES completion:nil];
}

二、通讯录联系人编辑
自定义的通讯录联系人列表,选中某一行联系人进行编辑。

  1. 导入#import <AddressBookUI/ABNewPersonViewController.h>框架。
  2. 遵守协议ABNewPersonViewControllerDelegate代理。
  3. 设置rightItem:
 //编辑联系人
    UIBarButtonItem  *rightBBItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(EditClicked:)];
    self.navigationItem.rightBarButtonItem = rightBBItem;
  1. 具体联系人编辑 的代码如下:
#pragma mark -  编辑联系人
-(void)EditClicked{
            ABNewPersonViewController  *picker = [[ABNewPersonViewController alloc]init];
          ABAddressBookRef   adressBook = ABAddressBookCreate();
        ABRecordID  recordID = _model.recordID;   //关键代码1
        picker.displayedPerson = ABAddressBookGetPersonWithRecordID(adressBook, recordID); //关键代码2
            picker.newPersonViewDelegate = self;
            UINavigationController  *navigation = [[UINavigationController alloc]initWithRootViewController:picker];
            [self presentViewController:navigation animated:YES completion:^{
                //刷新UI,
            }];
}

5.遵守协议,实现代理方法

#pragma mark - ABNewPersonViewControllerDelegate
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(nullable ABRecordRef)person{
    [newPersonView dismissViewControllerAnimated:YES completion:nil];
}

三、 增加联系人和编辑联系人区别:

 ABAddressBookRef   adressBook = ABAddressBookCreate(); 
        ABRecordID  recordID = _model.recordID;    //每个联系人的标识
        picker.displayedPerson = ABAddressBookGetPersonWithRecordID(adressBook, recordID); 
上一篇下一篇

猜你喜欢

热点阅读