如何正确使用ABPersonViewController和ABPeoplePickerNavigationController来查看联系信息? [英] How to correctly use ABPersonViewController with ABPeoplePickerNavigationController to view Contact information?

查看:91
本文介绍了如何正确使用ABPersonViewController和ABPeoplePickerNavigationController来查看联系信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新9/2/10:此代码不再适用于iOS 4更新 - 它因内部断言而失败。现在,iPhone SDK参考库中提供了一个名为QuickContacts的优秀地址簿API示例。

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."

示例代码可在此处获得;它将帮助您解决此问题:
http:/ /developer.apple.com/iphone/library/samplecode/QuickContacts/Introduction/Intro.html

The example code is available here; it will help you solve this problem: http://developer.apple.com/iphone/library/samplecode/QuickContacts/Introduction/Intro.html

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

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. 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 的问题。我可以让 ABPersonViewController 在屏幕上动画很好,但它只显示联系人的照片,姓名和公司名称。没有显示联系人的其他字段。

I've got the ABPeoplePickerNavigationController working fine, but I'm running into a problem displaying the 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. None of the contact's other fields are displayed.

我正在使用 ABPersonViewController 中的'displayedProperties'元素告诉程序显示电话数字。这会产生一些奇怪的行为;当我选择一个没有分配电话号码的联系人时,联系人会在后台显示无电话号码(正如您所期望的那样),但是当选择一个有电话号码的联系人时,我得到的只是一个空白的联系页面(没有无电话号码文本)。

I'm using the 'displayedProperties' element in the ABPersonViewController to 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).

这是我正在使用的 ABPeoplePickerNavigationController委托类中的方法创建我的 PersonViewController 类,它实现了 ABPersonViewController接口:

Here's the method in my ABPeoplePickerNavigationController delegate class that I'm using to create my PersonViewController class, which implements the ABPersonViewController interface:

- (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

最后,这是我的 PersonViewController.m 创建 ABPersonViewController 以查看所选联系人:

Finally, here's my PersonViewController.m that's creating the ABPersonViewController to view the selected contact:

#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?

好吧,我想我越来越接近找到问题了。我很确定这是上面的displayContactInfo调用的一部分:

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:

[self setView:personController.view];

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

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.

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

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

推荐答案

对于任何自己遇到这个问题的人来说只是提前:我能够产品通过在其委托的 viewDidLoad()方法中实例化ABPersonViewController来实现正确的行为,如下所示:

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:

和以前一样,这是我的 ABPeoplePickerNavigationController委托的方法:

As before, here's my ABPeoplePickerNavigationController delegate's method:

- (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程序组织的许多细微差别)。

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).

这篇关于如何正确使用ABPersonViewController和ABPeoplePickerNavigationController来查看联系信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆