【发布时间】:2013-04-25 15:13:36
【问题描述】:
我在导航控制器中嵌入了一个视图控制器。它有一个按钮和一个表格视图。我需要将手机中的联系人加载到此视图控制器的表格视图中,但发生的情况是打开了一个显示联系人的新导航控制器。这是.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 数组。
【问题讨论】:
-
好吧,现在值正在进入数组...初始化是使用 self.array=[[NSMutableArray alloc] init] 完成的,这就是为什么复制时值可能没有进入数组的原因.但我仍然没有在表格视图中得到任何东西。我将发布表格的代码。
-
好的.. 委托和数据源存在一些问题。我是从情节提要中设置它们的。我删除了它并使用syncContacts中的代码设置它们。现在我在表格视图中获取联系人。但问题是:我在通讯录中有 2 个联系人,按下视图控制器上的按钮时,它会在屏幕上显示这 2 个名称。但是当我再次按下按钮时,这两个名称都会再次添加到视图中。我希望一个联系人只出现一次,无论按钮被按下多少次。
标签: ios objective-c xcode xcode4.5