通讯录的实现

来源:互联网 发布:php b2b2c 编辑:程序博客网 时间:2024/05/19 01:14

iOS 模拟器屏幕快照“2013-2-18 下午4.32.29”.png                        iOS 模拟器屏幕快照“2013-2-18 下午4.32.33”.png



//  需要导入  AddressBookUI.framework,AddressBook.framework  框架



//  ViewController.h

#import <UIKit/UIKit.h>

#import <AddressBookUI/AddressBookUI.h>


@interface ViewController :UIViewController

<ABPeoplePickerNavigationControllerDelegate>


@property (nonatomic,retain)UITextField *personField;


@end


//ViewController.m

#import "ViewController.h"


@implementation ViewController


@synthesize personField;


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    self.view.backgroundColor = [UIColor grayColor];

    

    personField = [[UITextFieldalloc] initWithFrame:CGRectMake(10,50,200,30)];

    personField.backgroundColor = [UIColorwhiteColor];

    [self.viewaddSubview:personField];

    [personFieldrelease];

    

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button.frame =CGRectMake(220,50, 90, 30);

    [button setTitle:@"选择联系人" forState:UIControlStateNormal];

    [button addTarget:selfaction:@selector(selectPerson:)forControlEvents:UIControlEventTouchUpInside];

    

    [self.viewaddSubview:button];

}


- (void)selectPerson:(id)sender

{

    //创建选择联系人的导航控制器

    ABPeoplePickerNavigationController *peoplePickerController = [[ABPeoplePickerNavigationControlleralloc]init];

    //设置回调

    peoplePickerController.peoplePickerDelegate =self;

    //显示通讯录

    [selfpresentModalViewController:peoplePickerControlleranimated:YES];

}


//回调方法,当用户在通讯录上选择取消时调用

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker

{

    //关闭通讯录

    [selfdismissModalViewControllerAnimated:YES];

}


//回调方法,当用户在通讯录上选择一个联系人时调用

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

{

    //姓名

    NSString *name = (__bridgeNSString *)ABRecordCopyCompositeName(person);

    //在输入框上显示

    personField.text = name;  

    //关闭通讯录

    [selfdismissModalViewControllerAnimated:YES];

    returnNO;

}


//回调方法,当用户选择某个联系人的某个属性时调用

//iPhone有一些缺省操作,比如:当你选择某个电话时,就直接打过去.为了不触发这些操作,返回NO

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier

{

    returnNO;

}












原创粉丝点击