AddressBook UI contact 联系簿(UI) 官方demo

来源:互联网 发布:cnnic管理的通用域名 编辑:程序博客网 时间:2024/06/06 20:33

转载请说明:(谢谢)

http://blog.csdn.net/a21064346/article/details/8021842

点击打开链接

对于系统的contact 联系簿的ViewController 如何使用,下面又一个官方的demo,大家可以拿去看看。

这个东西,找了一段时间。其实不是很难,配合自己写的数据模型,很容易就能做出好的功能。只不过UI这块就要差一点了。

下面是一个类的h和m文件,大家自己把他搭建好,app delegate自己搭下,加入到一个NavigationController中即可。不需要其他的controller,只需要在导入两个系统的frame。

对于一些 contact 联系簿的controller,这里可以调用,不需要再去自己设计出 一个 很复杂的 contact了(本人尝试写了3000+的ui逻辑还没有实现它的全部功能,如果有人写出来了,或者网上有代码,希望可以学习下:),在此感激不尽)


#import <UIKit/UIKit.h>#import <AddressBook/AddressBook.h>#import <AddressBookUI/AddressBookUI.h>@interface QuickContactsViewController : UITableViewController < ABPeoplePickerNavigationControllerDelegate, ABPersonViewControllerDelegate,     ABNewPersonViewControllerDelegate,                 ABUnknownPersonViewControllerDelegate>{NSMutableArray *menuArray;}@property (nonatomic, retain) NSMutableArray *menuArray;-(void)showPeoplePickerController;-(void)showPersonViewController;-(void)showNewPersonViewController;-(void)showUnknownPersonViewController;@end

#import "QuickContactsViewController.h"enum TableRowSelected {kUIDisplayPickerRow = 0,kUICreateNewContactRow,kUIDisplayContactRow,kUIEditUnknownContactRow};// Height for the Edit Unknown Contact row#define kUIEditUnknownContactRowHeight 81.0@implementation QuickContactsViewController@synthesize menuArray;#pragma mark Load views// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad {[super viewDidLoad];// Load data from the plist fileNSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];self.menuArray = [NSMutableArray arrayWithContentsOfFile:plistPath];}#pragma mark Unload views- (void)viewDidUnload {self.menuArray = nil;    [super viewDidUnload];}#pragma mark Table view methods- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return [menuArray count];}// Customize the number of rows in the table view.- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return 1;}// Customize the appearance of table view cells.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *CellIdentifier = @"Cell";    UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (aCell == nil){// Make the Display Picker and Create New Contact rows look like buttonsif (indexPath.section < 2){aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];aCell.textLabel.textAlignment = UITextAlignmentCenter;}else{aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];aCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;aCell.detailTextLabel.numberOfLines = 0;// Display descriptions for the Edit Unknown Contact and Display and Edit Contact rows aCell.detailTextLabel.text = [[menuArray objectAtIndex:indexPath.section] valueForKey:@"description"];}}aCell.textLabel.text = [[menuArray objectAtIndex:indexPath.section] valueForKey:@"title"];return aCell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {switch (indexPath.section){case kUIDisplayPickerRow:[self showPeoplePickerController];break;case kUICreateNewContactRow:[self showNewPersonViewController];break;case kUIDisplayContactRow:[self showPersonViewController];break;case kUIEditUnknownContactRow:[self showUnknownPersonViewController];break;default:[self showPeoplePickerController];break;}}#pragma mark TableViewDelegate method- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{// Change the height if Edit Unknown Contact is the row selectedreturn (indexPath.section==kUIEditUnknownContactRow) ? kUIEditUnknownContactRowHeight : tableView.rowHeight;}#pragma mark Show all contacts// Called when users tap "Display Picker" in the application. Displays a list of contacts and allows users to select a contact from that list.// The application only shows the phone, email, and birthdate information of the selected contact.-(void)showPeoplePickerController{ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];    picker.peoplePickerDelegate = self;// Display only a person's phone, email, and birthdateNSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],     [NSNumber numberWithInt:kABPersonEmailProperty],    [NSNumber numberWithInt:kABPersonBirthdayProperty], nil];picker.displayedProperties = displayedItems;// Show the picker [self presentModalViewController:picker animated:YES];    [picker release];}#pragma mark Display and edit a person// Called when users tap "Display and Edit Contact" in the application. Searches for a contact named "Appleseed" in // in the address book. Displays and allows editing of all information associated with that contact if// the search is successful. Shows an alert, otherwise.-(void)showPersonViewController{// Fetch the address book ABAddressBookRef addressBook = ABAddressBookCreate();// Search for the person named "Appleseed" in the address bookNSArray *people = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook, CFSTR("1"));// Display "Appleseed" information if found in the address book if ((people != nil) && [people count]){ABRecordRef person = (ABRecordRef)[people objectAtIndex:0];ABPersonViewController *picker = [[[ABPersonViewController alloc] init] autorelease];picker.personViewDelegate = self;picker.displayedPerson = person;// Allow users to edit the person’s informationpicker.allowsEditing = YES;[self.navigationController pushViewController:picker animated:YES];}else {// Show an alert if "Appleseed" is not in ContactsUIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not find Appleseed in the Contacts application"    delegate:nil   cancelButtonTitle:@"Cancel"   otherButtonTitles:nil];[alert show];[alert release];}[people release];CFRelease(addressBook);}#pragma mark Create a new person// Called when users tap "Create New Contact" in the application. Allows users to create a new contact.-(void)showNewPersonViewController{ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];picker.newPersonViewDelegate = self;UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];[self presentModalViewController:navigation animated:YES];[picker release];[navigation release];}#pragma mark Add data to an existing person// Called when users tap "Edit Unknown Contact" in the application. -(void)showUnknownPersonViewController{ABRecordRef aContact = ABPersonCreate();CFErrorRef anError = NULL;ABMultiValueRef email = ABMultiValueCreateMutable(kABMultiStringPropertyType);bool didAdd = ABMultiValueAddValueAndLabel(email, @"John-Appleseed@mac.com", kABOtherLabel, NULL);if (didAdd == YES){ABRecordSetValue(aContact, kABPersonEmailProperty, email, &anError);if (anError == NULL){ABUnknownPersonViewController *picker = [[ABUnknownPersonViewController alloc] init];picker.unknownPersonViewDelegate = self;picker.displayedPerson = aContact;picker.allowsAddingToAddressBook = YES;    picker.allowsActions = YES;picker.alternateName = @"John Appleseed";picker.title = @"John Appleseed";picker.message = @"Company, Inc";[self.navigationController pushViewController:picker animated:YES];[picker release];}else {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not create unknown user"    delegate:nil   cancelButtonTitle:@"Cancel"  otherButtonTitles:nil];[alert show];[alert release];}}CFRelease(email);CFRelease(aContact);}#pragma mark ABPeoplePickerNavigationControllerDelegate methods// Displays the information of a selected person- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{return YES;}// Does not allow users to perform default actions such as dialing a phone number, when they select a person property.- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{return NO;}// Dismisses the people picker and shows the application when users tap Cancel. - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;{[self dismissModalViewControllerAnimated:YES];}#pragma mark ABPersonViewControllerDelegate methods// Does not allow users to perform default actions such as dialing a phone number, when they select a contact property.- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue{return NO;}#pragma mark ABNewPersonViewControllerDelegate methods// Dismisses the new-person view controller. - (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person{[self dismissModalViewControllerAnimated:YES];}#pragma mark ABUnknownPersonViewControllerDelegate methods// Dismisses the picker when users are done creating a contact or adding the displayed person properties to an existing contact. - (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownPersonView didResolveToPerson:(ABRecordRef)person{[self dismissModalViewControllerAnimated:YES];}// Does not allow users to perform default actions such as emailing a contact, when they select a contact property.- (BOOL)unknownPersonViewController:(ABUnknownPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person    property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{return NO;}#pragma mark Memory management- (void)dealloc {[menuArray release];    [super dealloc];}@end

原创粉丝点击