0

我对 AddressBook 框架有奇怪的行为。我正在以这种方式创建和显示人员选择器控制器:

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonFirstNameProperty], [NSNumber numberWithInt:kABPersonLastNameProperty], nil];
picker.displayedProperties = displayedItems;
[self presentViewController: picker animated:YES completion: nil];

然后在委托中,我有以下内容:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    myPersonRec = person;
    [self dismissViewControllerAnimated:YES completion: nil];
    return NO;
}

稍后当我尝试访问 myPersonRec 属性时,我只能读取名字和姓氏(显示在人员选择器中),所有其他属性都为零。如果我将返回值更改为YES,我将获得电话号码和所有东西,但无论如何都无法获得人的图像。我究竟做错了什么?

4

1 回答 1

0

找到问题源:

ABPerson对象不会一次全部加载到内存中。它被部分加载,并在需要时加载新字段或图像,并且要正确加载,该对象应绑定到现有ABAddressBook实例。当我使用人员选择器视图控制器时,它引用了它自己的ABAddressBook实例,该实例在选择器被解除后释放,因此在视图控制器解除后我无法获得任何新数据(但之前加载的条目仍然可以访问)。

为了解决这个问题,我ABAddressBook在我的模型类单例中创建了一个共享实例,并在需要时使用它。因为我只是从通讯录中阅读,所以应该是很好的解决方案。

所以,选择器创建代码最终应该是这样的:

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonFirstNameProperty], [NSNumber numberWithInt:kABPersonLastNameProperty], nil];
picker.displayedProperties = displayedItems;
picker.addressBook = [[MyModel instance] sharedAddressBookRef];
[self presentViewController: picker animated:YES completion: nil];
于 2012-10-16T14:02:12.460 回答