9/2/10更新:此代码在iOS 4更新时不再有效--它因内部断言而失败。现在,在名为“QuickContacts”的iPhone开发工具包参考库中提供了一个很棒的Address Book API示例。
示例代码可以在这里获得;它将帮助您解决这个问题:http://developer.apple.com/iphone/library/samplecode/QuickContacts/Introduction/Intro.html
我正在尝试向我的应用程序添加一个功能,允许用户从ABPeoplePickerNavigationController,中选择联系人,然后显示与他们选择的联系人相对应的ABPersonViewController。在这一点上,我希望用户能够单击联系人的电话号码,并让我的应用程序使用自定义行为进行响应。
我已经让ABPeoplePickerNavigationController正常工作了,但是我在显示ABPersonViewController.时遇到了问题我可以很好地让ABPersonViewController在屏幕上显示动画,但它只显示联系人的照片、姓名和公司名称。不会显示该联系人的任何其他字段。
我在ABPersonViewController中使用'displayedProperties‘元素来告诉程序显示电话号码。这就产生了一些奇怪的行为;当我选择一个没有分配电话号码的联系人时,该联系人在后台显示"No Phone Numbers“(如您所料),但当选择一个有电话号码的联系人时,我得到的只是一个空白的联系人页面(没有"No phone Numbers”文本)。
下面是我用来创建PersonViewController类的ABPeoplePickerNavigationController委托类中的方法,它实现了ABPersonViewController接口:
- (BOOL) peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
BOOL returnState = NO;
PersonViewController *personView = [[PersonViewController alloc] init];
[personView displayContactInfo:person];
[peoplePicker pushViewController:personView animated:YES];
[personView release];
return returnState;
}
下面是我的PersonViewController.h头文件:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AddressBookUI/AddressBookUI.h>
@interface PersonViewController : UIViewController <ABPersonViewControllerDelegate>
{
}
- (void) displayContactInfo: (ABRecordRef)person;
@end
最后,下面是创建用于查看所选联系人的ABPersonViewController的PersonViewController.m:
#import "PersonViewController.h"
@implementation PersonViewController
- (void) displayContactInfo: (ABRecordRef)person
{
ABPersonViewController *personController = [[ABPersonViewController alloc] init];
personController.personViewDelegate = self;
personController.allowsEditing = NO;
personController.displayedPerson = person;
personController.addressBook = ABAddressBookCreate();
personController.displayedProperties = [NSArray arrayWithObjects:
[NSNumber numberWithInt:kABPersonPhoneProperty],
nil];
[self setView:personController.view];
[[self navigationController] pushViewController:personController animated:YES];
[personController release];
}
- (BOOL) personViewController:(ABPersonViewController*)personView shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue
{
return YES;
}
@end
有没有人知道为什么我看到这个空白的联系人屏幕,而不是一个有可点击的电话号码字段的屏幕?
好了,我想我快找到问题所在了。我非常确定这是上面displayContactInfo调用的这一部分:
[self setView:personController.view];
如果我省略了这一行,当我单击一个联系人时,我看到的只是一个空白屏幕。ABPeoplePickerNavigationController正在将PersonViewController推送到NavigationController堆栈上。然后,PersonViewController实例化一个ABPersonViewController对象,但是由于某种原因,ABPersonViewController从未正确地添加到NavigationController堆栈中。
有谁知道如何将ABPersonViewController添加到堆栈中,而不仅仅是PersonViewController?
发布于 2009-08-31 07:17:21
对于遇到这个问题的任何人,我只想提个醒:我能够通过在其委托的viewDidLoad()方法中实例化ABPersonViewController来产生正确的行为,如下所示:
和前面一样,下面是我的ABPeoplePickerNavigationController委托的方法:
- (BOOL) peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
BOOL returnState = NO;
PersonViewController *personView = [[PersonViewController alloc] init];
[peoplePicker pushViewController:personView animated:YES];
[personView displayContactInfo:person];
[personView release];
return returnState;
}
这是我的PersonViewController.h (ABPersonViewController委托)头文件:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AddressBookUI/AddressBookUI.h>
@interface PersonViewController : UIViewController <ABPersonViewControllerDelegate>
{
ABPersonViewController *personController;
}
- (void) displayContactInfo: (ABRecordRef)person;
@end
最后,下面是委托的实现(PersonViewController.m):
#import "PersonViewController.h"
@implementation PersonViewController
- (void) viewDidLoad
{
}
- (void) viewDidUnload
{
[personController release];
}
- (void) displayContactInfo: (ABRecordRef)person
{
personController = [[ABPersonViewController alloc] init];
[personController setDisplayedPerson:person];
[personController setPersonViewDelegate:self];
[personController setAllowsEditing:NO];
personController.addressBook = ABAddressBookCreate();
personController.displayedProperties = [NSArray arrayWithObjects:
[NSNumber numberWithInt:kABPersonPhoneProperty],
nil];
[self setView:personController.view];
}
- (BOOL) personViewController:(ABPersonViewController*)personView shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue
{
// This is where you pass the selected contact property elsewhere in your program
[[self navigationController] dismissModalViewControllerAnimated:YES];
return NO;
}
@end
希望这能对某些人有所帮助。AddressBook UI框架对我来说有点难以理解(尽管我是iPhone开发的新手,所以我还在学习iPhone程序组织的许多细微差别)。
https://stackoverflow.com/questions/1320931
复制相似问题