的ABPeoplePickerNavigationController。需要在同一视图控制器中显示联系人

时间:2013-04-25 15:13:37

标签: ios objective-c xcode xcode4.5

我在导航控制器中嵌入了一个View Controller。它有一个按钮和一个表视图。我需要手机中的联系人加载到此View 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;
}

我猜这是非常标准的代码。如何在具有按钮的视图控制器的表视图中显示联系人,而不是在不同的控制器中?

好的,我现在已经这样做了:

 - (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(@“%@”,联系人)被执行时,各个联系人被获取并显示在日志中。但是,当我将联系人复制到phoneContacts数组(可变)时,它不会复制。我尝试过addObject,insertObject:atIndex:,replaceObjectAtIndex:withObject:etc,但仍然是phoneContacts数组保持为null。它已在didViewLoad()中初始化。由于没有任何内容存储在phoneContacts数组中,因此表视图也没有被填充,因为它使用phoneContacts数组。

4 个答案:

答案 0 :(得分:1)

您将需要以编程方式而不是通过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

答案 1 :(得分:0)

  

如何在具有按钮

的视图控制器的表格视图中显示联系人

根据定义,您不能使用ABPeoplePickerNavigationController,因为显示联系人的内容是新的视图控制器。新视图控制器=不同视图。换句话说,这是使用内置的ABPeoplePickerNavigationController节省自己不必自己做大量工作的代价。

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

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

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

答案 2 :(得分: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)];
}

虽然这种方法有点笨拙,但它比实现整个自定义联系人选择器更有意义,只是为了将它嵌入到另一个控制器中。

答案 3 :(得分:-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];
 }];
}