gpt4 book ai didi

ios - ABPeoplePickerNavigationController。需要在同一个 View Controller 中显示联系人

转载 作者:行者123 更新时间:2023-11-28 17:54:38 26 4
gpt4 key购买 nike

我有一个嵌入在导航 Controller 中的 View Controller 。它有一个按钮和一个表格 View 。我需要将手机中的联系人加载到此 View Controller 的 TableView 中,但发生的情况是打开了一个显示联系人的新导航 Controller 。这是 .m 文件的代码:

- (IBAction)showContacts:(id)sender
{
ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;

// picker.modalPresentationStyle = UIModalPresentationCurrentContext;
//picker.modalInPopover = YES;
// [self.navigationController presentModalViewController:picker animated:YES];
[self presentModalViewController:picker animated:YES];
}

- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}


- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {

// [self displayPerson:person];
[self dismissModalViewControllerAnimated:YES];

return NO;
}

- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}

我猜这是非常标准的代码。如何让联系人显示在具有按钮的 View Controller 的 TableView 中,而不是在不同的 Controller 中?

好的,现在我已经完成了:

 - (IBAction)syncContacts:(id)sender
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
for (int i = 0; i < ABAddressBookGetPersonCount(addressBook); i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
NSString *contact = (NSString *)CFBridgingRelease(ABRecordCopyCompositeName(ref));
NSLog( @"%@", contact);
phoneContacts[i] = contact;
}
NSLog(@"%@",phoneContacts);
}

我已将方法的名称更改为 syncContacts(为方便起见)。当 NSLog(@"%@",contact) 被执行时,个人联系人将被获取并显示在日志中。但是当我将联系人复制到 phoneContacts 数组(可变)时,它并没有复制。我试过 addObject、insertObject:atIndex:、replaceObjectAtIndex:withObject: 等,但 phoneContacts 数组仍然为空。它已经在 didViewLoad() 中进行了初始化。由于 phoneContacts 数组中没有存储任何内容,因此 TableView 也没有被填充,因为它使用 phoneContacts 数组。

最佳答案

您将需要以编程方式而不是通过 AddressBookUI 访问 AddressBook 框架。您可以将 tableView 数据源设置为这样的结果:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
kCFAllocatorDefault,
CFArrayGetCount(people),
people
);

参见:http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone

关于ios - ABPeoplePickerNavigationController。需要在同一个 View Controller 中显示联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16218518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com