1

我有一个嵌入在导航控制器中的视图控制器。它有一个按钮和一个表格视图。我需要将手机中的联系人加载到此视图控制器的表格视图中,但发生的情况是打开了一个显示联系人的新导航控制器。这是 .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 数组。

4

4 回答 4

1

您将需要以编程方式访问 AddressBook 框架,而不是通过 AddressBookUI。您可以将 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

于 2013-04-25T15:25:05.903 回答
0

如何让联系人显示在具有按钮的视图控制器的表视图中

根据定义,如果您使用 ABPeoplePickerNavigationController,则不能,因为显示联系人的是一个新的视图控制器。新的视图控制器 = 不同的视图。换句话说,这是使用内置 ABPeoplePickerNavigationController 来避免自己做大量工作的代价。

另一种方法是发明自己的界面。您必须自己解析联系人数据库并呈现数据。这不是不可能的。这是我书中描述如何解析联系人数据库的部分:

http://www.aeth.com/iOSBook/ch31.html#_address_book_database

但老实说,我认为您应该接受使用 ABPeoplePickerNavigationController 所涉及的妥协——调整您的界面以按照 ABPeoplePickerNavigationController 想要使用的方式做事。你会为自己节省很多时间。

于 2013-04-25T15:24:02.663 回答
0

我通过将 ABPeoplePickerNavigationController 的视图嵌入到我自己的视图控制器中来完成这项工作:

- (void)embedContactsView {
    self.contactsController = [[ABPeoplePickerNavigationController alloc] init];
    [self.contactsController setPeoplePickerDelegate:self];

    //embed the view
    [self addChildViewController:self.contactsController];
    [self.view addSubview:self.contactsController.view];
    //if your view controller is a navigation controller make sure you
    //hide the nav bar or it will overlap the contacts search.
    [self.navigationController setNavigationBarHidden:YES];
}

您甚至可以更改联系人选择器的导航栏(例如隐藏取消按钮):

- (void)customiseContactsTabBar { //if this doesn't work be sure to call it in viewDidAppear
    //customise the apple picker a bit
    [self.contactsController.topViewController.navigationItem setTitle:@"Everyone"];
    self.contactsController.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(newButtonPressed)];
}

虽然这种方法有点 hacky,但它比实现一个完整的自定义联系人选择器更有意义,以便它可以嵌入到另一个控制器中。

于 2014-04-17T13:29:57.897 回答
-2
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
{
 [self dismissViewControllerAnimated:NO completion:^{
     ABPersonViewController *personController = [[ABPersonViewController alloc] init];

     [personController setDisplayedPerson:person];
     [personController setPersonViewDelegate:self];
     [personController setAllowsEditing:NO];
     personController.displayedProperties = [NSArray arrayWithObjects:
                                             [NSNumber numberWithInt:kABPersonPhoneProperty],
                                             nil];         
     [self.navigationController pushViewController:personController animated:YES];
 }];
}
于 2014-10-31T13:53:30.140 回答