Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何正确使用ABPersonViewController和ABPeoplePickerNavigationController查看联系信息?_Iphone_Ios_Addressbook - Fatal编程技术网

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

Iphone 如何正确使用ABPersonViewController和ABPeoplePickerNavigationController查看联系信息?,iphone,ios,addressbook,Iphone,Ios,Addressbook,更新9/2/10:这段代码在iOS 4更新后不再工作——它会由于内部断言而失败。现在,iPhone SDK参考库中有一个很棒的地址簿API示例,名为“QuickContacts” 这里提供了示例代码;它将帮助您解决此问题: 我正在尝试向我的应用程序添加一项功能,允许用户从联系人PickerNavigationController中选择联系人,然后该控制器将显示一个与他们选择的联系人对应的联系人视图控制器。在这一点上,我希望用户能够点击联系人的电话号码,并让我的应用程序以自定义行为响应 我的A

更新9/2/10:这段代码在iOS 4更新后不再工作——它会由于内部断言而失败。现在,iPhone SDK参考库中有一个很棒的地址簿API示例,名为“QuickContacts”

这里提供了示例代码;它将帮助您解决此问题:


我正在尝试向我的应用程序添加一项功能,允许用户从联系人PickerNavigationController中选择联系人,然后该控制器将显示一个与他们选择的联系人对应的联系人视图控制器。在这一点上,我希望用户能够点击联系人的电话号码,并让我的应用程序以自定义行为响应

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

我正在使用ABPersonViewController中的“displayedProperties”元素来告诉程序显示电话号码。这造成了一些奇怪的行为;当我选择一个没有指定电话号码的联系人时,该联系人会在后台显示“no phone number”(没有电话号码)(如您所料),但当选择一个确实有电话号码的联系人时,我得到的只是一个空白的联系人页面(没有“no phone number”(没有电话号码)文本)

下面是我的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;
}
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AddressBookUI/AddressBookUI.h>

@interface PersonViewController : UIViewController <ABPersonViewControllerDelegate> 
{
    ABPersonViewController *personController;
}

- (void) displayContactInfo: (ABRecordRef)person;

@end
#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
这是我的PersonViewController.h头文件:

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

@interface PersonViewController : UIViewController <ABPersonViewControllerDelegate> 
{

}

- (void) displayContactInfo: (ABRecordRef)person;

@end
有人知道为什么我看到的是空白的联系人屏幕而不是带有可点击电话号码字段的屏幕吗


好吧,我想我离找到问题越来越近了。我敢肯定这是上面displayContactInfo通话的一部分:

[self setView:personController.view];
当我省略这一行时,当我单击联系人时,我看到的只是一个空白屏幕。ABPeoplePickerNavigationController正在将PersonViewController推送到NavigationController堆栈上。PersonViewController然后实例化一个ABPersonViewController对象,但无论出于何种原因,ABPersonViewController始终无法正确添加到NavigationController堆栈中


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

这正好提醒了那些自己遇到这个问题的人:我能够通过在其委托的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代理)头文件:

- (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;
}
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AddressBookUI/AddressBookUI.h>

@interface PersonViewController : UIViewController <ABPersonViewControllerDelegate> 
{
    ABPersonViewController *personController;
}

- (void) displayContactInfo: (ABRecordRef)person;

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

感谢您发布本文。这是我能找到的最好的教程。现在我只需要了解如何从所选条目中获取人员编号:-)