ios 如何正确使用 ABPersonViewController 和 ABPeoplePickerNavigationController 查看联系人信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1320931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:19:14  来源:igfitidea点击:

How to correctly use ABPersonViewController with ABPeoplePickerNavigationController to view Contact information?

iphoneiosaddressbook

提问by Jason Machacek

Update 9/2/10: This code no longer works as of the iOS 4 update--it fails with an internal assertion. There is now a great Address Book API example available in the iPhone SDK Reference Library called "QuickContacts."

2010 年 9 月 2 日更新:此代码在 iOS 4 更新后不再有效——它因内部断言而失败。iPhone SDK 参考库中现在有一个很棒的地址簿 API 示例,称为“QuickContacts”。

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 ABPersonViewControllercorresponding to the contact they picked. 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.

我正在尝试向我的应用程序添加一项功能,允许用户从ABPeoplePickerNavigationController 中选择联系人,然后显示与他们选择的联系人对应的ABPersonViewController。那时,我希望用户能够点击联系人的电话号码并让我的应用程序以自定义行为进行响应。

I've got the ABPeoplePickerNavigationControllerworking fine, but I'm running into a problem displaying the ABPersonViewController. I can get the ABPersonViewControllerto animate onto the screen just fine, but it only displays the contact's photo, name, and company name. None of the contact's other fields are displayed.

我让ABPeoplePickerNavigationController工作正常,但我在显示ABPersonViewController 时遇到了问题。我可以让ABPersonViewController动画到屏幕上就好了,但它只显示联系人的照片、姓名和公司名称。不显示联系人的其他字段。

I'm using the 'displayedProperties' element in the ABPersonViewControllerto tell the program to display phone numbers. 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).

我在ABPersonViewController 中使用“displayedProperties”元素来告诉程序显示电话号码。这会产生一些奇怪的行为;当我选择没有分配电话号码的联系人时,联系人会在背景中写入“无电话号码”(如您所期望的),但在选择有电话号码的联系人时,我得到的就是一个空白的联系页面(没有“无电话号码”文本)。

Here's the method in my ABPeoplePickerNavigationController delegateclass that I'm using to create my PersonViewControllerclass, 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.hheader 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.mthat's creating the ABPersonViewControllerto 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. The PersonViewController then instantiates an ABPersonViewController object, but for whatever reason the ABPersonViewController never gets properly added to the NavigationController stack.

当我省略这一行时,当我单击联系人时,我看到的只是一个空白屏幕。ABPeoplePickerNavigationController 将 PersonViewController 推送到 NavigationController 堆栈上。然后 PersonViewController 实例化一个 ABPersonViewController 对象,但无论出于何种原因,ABPersonViewController 从未正确添加到 NavigationController 堆栈中。

Does anyone know how to add the ABPersonViewController to the stack, rather than just the PersonViewController?

有谁知道如何将 ABPersonViewController 添加到堆栈中,而不仅仅是 PersonViewController?

采纳答案by Jason Machacek

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'smethod:

和以前一样,这是我的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 frameworkwas 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).

希望这最终对某人有所帮助。该通讯录的UI框架是有点棘手,让我满脑子都在(虽然我是新iPhone的发展,所以我还在学习很多iPhone程序组织的细微差别)。