简体   繁体   English

如何正确使用ABPersonViewController与ABPeoplePickerNavigationController查看联系信息?

[英]How to correctly use ABPersonViewController with ABPeoplePickerNavigationController to view Contact information?

Update 9/2/10: This code no longer works as of the iOS 4 update--it fails with an internal assertion. 更新9/2/10:此代码不再适用于iOS 4更新 - 它失败并带有内部断言。 There is now a great Address Book API example available in the iPhone SDK Reference Library called "QuickContacts." 现在,iPhone SDK参考库中提供了一个名为“QuickContacts”的优秀通讯簿API示例。

The example code is available here; 示例代码可在此处获得; it will help you solve this problem: http://developer.apple.com/iphone/library/samplecode/QuickContacts/Introduction/Intro.html 它将帮助您解决此问题: http//developer.apple.com/iphone/library/samplecode/QuickContacts/Introduction/Intro.html


I'm attempting to add a feature to my app that allows the user to select a contact from an ABPeoplePickerNavigationController , which then displays an ABPersonViewController corresponding to the contact they picked. 我正在尝试向我的应用添加一项功能,允许用户从ABPeoplePickerNavigationController中选择一个联系人,然后显示与他们选择的联系人对应的ABPersonViewController At that point, I want the user to be able to click on a contact's phone number and have my app respond with custom behavior. 此时,我希望用户能够点击联系人的电话号码并让我的应用程序响应自定义行为。

I've got the ABPeoplePickerNavigationController working fine, but I'm running into a problem displaying the ABPersonViewController . 我已经让ABPeoplePickerNavigationController工作正常了,但是我遇到了显示ABPersonViewController的问题。 I can get the ABPersonViewController to animate onto the screen just fine, but it only displays the contact's photo, name, and company name. 我可以让ABPersonViewController在屏幕上动画很好,但它只显示联系人的照片,名称和公司名称。 None of the contact's other fields are displayed. 没有显示联系人的其他字段。

I'm using the 'displayedProperties' element in the ABPersonViewController to tell the program to display phone numbers. 我正在使用ABPersonViewController中的'displayedProperties'元素告诉程序显示电话号码。 This creates some strange behavior; 这会产生一些奇怪的行为; when I select a contact that has no phone numbers assigned, the contact shows up with "No Phone Numbers" written in the background (as you'd expect), but when selecting a contact that does have a phone number, all I get is a blank contact page (without the "No Phone Numbers" text). 当我选择一个没有分配电话号码的联系人时,联系人会在后台显示“无电话号码”(正如您所期望的那样),但是当选择一个有电话号码的联系人时,我得到的只是一个空白的联系页面(没有“无电话号码”文本)。

Here's the method in my ABPeoplePickerNavigationController delegate class that I'm using to create my PersonViewController class, which implements the ABPersonViewController interface: 下面是我的ABPeoplePickerNavigationController委托类的方法,我使用创建我PersonViewController类,它实现了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;
}

Here's my PersonViewController.h header file: 这是我的PersonViewController.h头文件:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AddressBookUI/AddressBookUI.h>

@interface PersonViewController : UIViewController <ABPersonViewControllerDelegate> 
{

}

- (void) displayContactInfo: (ABRecordRef)person;

@end

Finally, here's my PersonViewController.m that's creating the ABPersonViewController to view the selected contact: 最后,这是我的PersonViewController.m ,它创建了ABPersonViewController来查看所选的联系人:

#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

Does anyone have any idea as to why I'm seeing this blank Contact screen instead of one with clickable phone number fields? 有没有人知道为什么我看到这个空白的联系人屏幕而不是一个可点击的电话号码字段?


Alright, I think I'm getting closer to finding the problem. 好吧,我想我越来越接近找到问题了。 I'm fairly sure it's with this part of the displayContactInfo call above: 我很确定这是上面displayContactInfo调用的这一部分:

[self setView:personController.view];

When I omit this line, all I see is a blank screen when I click a contact. 当我省略这一行时,当我点击一个联系人时,我看到的只是一个空白屏幕。 The ABPeoplePickerNavigationController is pushing the PersonViewController onto the NavigationController stack. ABPeoplePickerNavigationController将PersonViewController推送到NavigationController堆栈。 The PersonViewController then instantiates an ABPersonViewController object, but for whatever reason the ABPersonViewController never gets properly added to the NavigationController stack. 然后PersonViewController实例化一个ABPersonViewController对象,但无论出于何种原因,ABPersonViewController永远不会被正确添加到NavigationController堆栈中。

Does anyone know how to add the ABPersonViewController to the stack, rather than just the PersonViewController? 有谁知道如何将ABPersonViewController添加到堆栈,而不仅仅是PersonViewController?

Just a heads-up to anyone who runs into this problem themselves: I was able to product the correct behavior by instantiating the ABPersonViewController in its delegate's viewDidLoad() method as below: 只是对自己遇到这个问题的任何人提出挑战:我能够通过在其委托的viewDidLoad()方法中实例化ABPersonViewController来产生正确的行为,如下所示:

As before, here's my ABPeoplePickerNavigationController delegate's method: 和以前一样,这是我的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;
}

Here's my PersonViewController.h (ABPersonViewController delegate) header file: 这是我的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

Finally, here's the delegate's implementation (PersonViewController.m): 最后,这是委托的实现(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

Hopefully this ends up being helpful for someone. 希望这最终会对某人有所帮助。 The AddressBook UI framework was a bit tricky for me to wrap my head around (although I'm new to iPhone development so I'm still learning a lot of the nuances of iPhone program organization). AddressBook UI框架对我来说有点棘手(虽然我是iPhone开发的新手,所以我还在学习iPhone程序组织的许多细微差别)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有可编辑的ABPersonViewController的ABPeoplePickerNavigationController - ABPeoplePickerNavigationController with editable ABPersonViewController 是否可以使用ABPersonViewController或ABUknownPersonViewController显示联系人信息? - Is it possible to use a ABPersonViewController or ABUknownPersonViewController for displaying contact info? 加载ABPersonViewController之后如何以编程方式调用ABPersonViewController编辑视图 - How to programmatically call ABPersonViewController edit view after loading ABPersonViewController ABPersonViewController用于显示联系人的用法 - ABPersonViewController Usage for displaying contact 如何在核心数据堆栈中的客户端上使用ABPeoplePickerNavigationController - How to use ABPeoplePickerNavigationController on clients in core data stack 我应该使用ABPersonViewController还是ABUnknownPersonViewController - Should I use ABPersonViewController or ABUnknownPersonViewController 显示iPhone联系人选择器的本地化版本(ABPeoplePickerNavigationController) - Displaying a localised version of iPhone Contact Picker (ABPeoplePickerNavigationController) ABPeoplePickerNavigationController在单个cel中显示所有联系人数据 - ABPeoplePickerNavigationController display all contact data in a single cel ABPeoplePickerNavigationController - ABPeoplePickerNavigationController 如何将leftBarButtonItem添加到ABPeoplePickerNavigationController? - How to add a leftBarButtonItem to ABPeoplePickerNavigationController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM