我有一个嵌入在导航控制器中的视图控制器。它有一个按钮和一个表格视图。我需要将手机中的联系人加载到此视图控制器的表格视图中,但发生的情况是打开了一个显示联系人的新导航控制器。这是 .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;
}
我猜这是非常标准的代码。如何让联系人显示在具有按钮的视图控制器的表视图中,而不是在不同的控制器中?
好的,现在我已经这样做了:
- (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 数组中,因此表格视图也没有被填充,因为它使用了 phoneContacts 数组。