【问题标题】:ABPeoplePickerNavigationController. Need to show contacts in the same view controllerABPeoplePickerNavigationController。需要在同一个视图控制器中显示联系人
【发布时间】: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


【解决方案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

【讨论】:

    【解决方案2】:

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

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

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

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

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

    【讨论】:

    • 是的,我明白......但不幸的是,这是我项目的要求......我设法让它们在同一个视图控制器上,但我有一些问题......我在上面提到过..
    【解决方案3】:

    我通过将 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)];
    }
    

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

    【讨论】:

      【解决方案4】:
      - (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];
       }];
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-24
        • 2012-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-21
        • 2018-07-27
        相关资源
        最近更新 更多