1. Using the ABPersonViewController Class
The ABPersonViewController
class allows you to present the details of a specific record to the
user. As with its other sisters, you need to create it and initialize
it. Next, you set its delegate (one method to implement). After that,
you configure some of its options. The allowsEditing property is set to YES if you want the user to edit the record. NO is the default. The displayedPerson must be set to the person record you want to show. The displayedProperties
is an array of all properties you want to show. If you allow editing,
then the other properties will show as well while editing.
If you want to highlight a given property, use the setHighlightedItemForProperty:with-Identifier: method which is declared as follows:
- (void)setHighlightedItemForProperty:(ABPropertyID)property
withIdentifier:(ABMultiValueIdentifier)identifier;
The property
parameter specifies the property identifier you want to highlight. If
the property is single-value, the second parameter is ignored. If, on
the other hand, the property is multi-value (e.g., phone), the identifier specifies which value to highlight. You can highlight as many values as you want.
Listing 1
shows how one can setup and invoke a person view controller. In this
example, we retrieve the first person record and show it. The displayed
properties are the phone and email properties. The method also
highlights the phone value with identifier 0.
Example 1. Showing a person record using the ABPersonViewController class.
-(void)buttonPushed{
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *allPeople =
(NSArray*) ABAddressBookCopyArrayOfAllPeople(addressBook);
if(allPeople.count){
ABRecordRef person = (ABRecordRef)[allPeople objectAtIndex:0];
ABPersonViewController *personViewController =
[[[ABPersonViewController alloc] init] autorelease];
personViewController.personViewDelegate = self;
personViewController.displayedPerson = person;
personViewController.displayedProperties =
[NSArray arrayWithObjects:
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonPhoneProperty], nil];
[personViewController
setHighlightedItemForProperty:kABPersonPhoneProperty
withIdentifier:0];
[self presentModalViewController:personViewController animated:YES];
}
[allPeople release];
CFRelease(addressBook);
}
|
Listing 2
shows the delegate method of the person view controller. If the
property selected by the user is an email property, we log it, disallow
default action, and dismiss the controller. Otherwise, we dismiss the
controller and allow default action to occur.
Example 2. The delegate method of a person view controller.
- (BOOL)
personViewController:(ABPersonViewController *)personViewController
shouldPerformDefaultActionForPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifierForValue{
if(property == kABPersonEmailProperty){
ABMultiValueRef emailProperty = ABRecordCopyValue(person, property);
NSInteger emailndex =
ABMultiValueGetIndexForIdentifier(emailProperty, identifierForValue);
NSString *email =
(NSString *)ABMultiValueCopyValueAtIndex(emailProperty, emailndex);
NSLog(@"The email selected is %@:", email);
CFRelease(emailProperty);
[email release];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
else{
[self dismissModalViewControllerAnimated:YES];
return YES;
}
}
|
Figure 1 shows a view from the person view controller.
2. Using the ABNewPersonViewController Class
If you want the user to create a new person record, you can use the ABNewPersonViewController
controller. You create and initialize the controller and set the
delegate. The controller is then pushed on the stack. The following
shows a typical invocation:
ABNewPersonViewController *newPersonViewController =
[[[ABNewPersonViewController alloc] init] autorelease];
newPersonViewController.newPersonViewDelegate = self;
[self.navigationController
pushViewController:newPersonViewController animated:YES];
The only delegate method is newPersonViewController:didCompleteWithNewPerson:. You should dismiss the controller and inspect the new person record. Note that the person will be added to the address book by the time this method is called. If the user cancelled, the person parameter is NULL.
- (void)
newPersonViewController:(ABNewPersonViewController *)newPersonView
didCompleteWithNewPerson:(ABRecordRef)person{
[self.navigationController popViewControllerAnimated:YES];
}
Figure 2 shows the view of ABNewPersonViewController controller.