11

我已经快要绝望了,因为我已经寻找了数周的解决方案。

问题很简单:

  • 通过 ABPeoplePickerNavigationController(作为 ModalView),应该选择一个人。
  • 然后只有(例如)邮件地址应该被显示并且用户应该选择一个。
  • 在选择了一个邮件地址之后,应该只显示(例如)电话号码,并且用户应该选择一个。

直到第三个方面的解决方案是众所周知的:

- (IBAction)importFromAddressBook 
{
    ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker 
{
    [self dismissModalViewControllerAnimated:YES];
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker 
      shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{
    [peoplePicker setDisplayedProperties:[NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonEmailProperty]]];
    return YES;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier 
{
    //===PROBLEM=== Now I do have a mail address and I want to have a phone number right afterwards.

    //Something like this would be straightforward, but the view does not change this way:
    [peoplePicker setDisplayedProperties:[NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]]];
    //Here misses the important code.


    //And only when I also got a phone number through this or a similar method I want to call:
    [peoplePicker dismissModalViewControllerAnimated:YES];

    //I do not want to start default behaviour with the mailaddress and/or phone number:
    return NO;
}

正确的做法似乎是在ModalView的NavigationController上推了一个类似的peoplePicker View,但是我不知道怎么做。

如果有人有任何想法,那就太好了!

如果你想看到这样的行为,你可以看看亚马逊应用程序:如果你完成订单的第一步,你可以通过这种方式选择送货地址:从联系人列表 -> 选择一个人 ->选择地址 -> 选择电话号码。在那里,一切(似乎)都发生在模态视图中,只有一个比上面显示的标准代码多一级的导航层次结构。

4

4 回答 4

15

我想这可能是你想要的:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person 
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier 
{

    ABPersonViewController *controller = [[ABPersonViewController alloc] init];
    controller.displayedPerson = person;
    controller.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
    controller.personViewDelegate = self;
    [peoplePicker pushViewController:controller animated:YES];
    [controller release];
    return NO;
}

- (BOOL)personViewController:(ABPersonViewController *)personViewController 
shouldPerformDefaultActionForPerson:(ABRecordRef)person 
                    property:(ABPropertyID)property
                  identifier:(ABMultiValueIdentifier)identifierForValue
{
    ABMutableMultiValueRef multi = ABRecordCopyValue(person, property);
    CFStringRef phone = ABMultiValueCopyValueAtIndex(multi, identifierForValue);
    NSLog(@"phone %@", (NSString *)phone);
    CFRelease(phone);

    ABPeoplePickerNavigationController *peoplePicker = (ABPeoplePickerNavigationController *)personViewController.navigationController;
    [peoplePicker dismissModalViewControllerAnimated:YES];
    return NO;
}

这个想法是,创建另一个 ABPersonViewController 实例,并让你的人员选择器推送它,因为 ABPeoplePickerNavigationController 是 NSPeoplePickerNavigationController 的子类。

于 2010-03-16T20:54:05.250 回答
1

在我的 iPhone 应用程序 Pastie 中,我采用了不同的方法。(来源:manicwave.com替代文字

我使用 peoplePicker 选择人员,然后打开联系人(人员)编辑器。

这只是一个简单的视图:

联系人姓名电话号码>默认为第一个电话号码电子邮件地址>默认为第一个电子邮件地址

每个电话号码和电子邮件地址都会显示另一个视图,显示电话或电子邮件地址列表,在当前选定的旁边有一个复选标记。

我将此视图用于联系人的初始设置以及后续编辑。

于 2010-02-28T04:45:48.497 回答
1

以下方法应返回 NO:

- (BOOL)peoplePickerNavigationController: 
  (ABPeoplePickerNavigationController*)peoplePicker 
  shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{
  ...
  return NO;
}

这将允许调用您的下一个方法 (peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:)。

于 2010-07-20T15:16:22.360 回答
1

在建议的答案中缺少一个版本

CFR 释放(多);

如果没有这个版本,就会发生泄漏。或者至少根据 Xcode 中的 Build and Analyze ....

于 2010-05-22T07:59:22.623 回答