3

我的应用程序需要将自定义类的实例与 iPhone 通讯录中的联系人记录相关联。当我展示ABPeoplePickerNavigationController并允许用户选择现有联系人时,一切都很好。问题是没有明显的方法可以让用户轻松添加联系人记录,如果他们正在寻找的联系人记录不存在于他们的地址簿中。

人们如何以一种对用户来说简单直观的方式从ABPeoplePickerNavigationControllerABNewPersonViewController

4

3 回答 3

6

您可以像这样创建一个 UIBarButton 并将其添加到 ABPeoplePickerNavigationController 的 UINavigationBar。

    peoplePicker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson:)];

-(IBAction)addPerson:(id)sender{
    ABNewPersonViewController *view = [[ABNewPersonViewController alloc] init];
    view.newPersonViewDelegate = self;
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:view];
    [self.picker presentModalViewController:nc animated:YES];
}

我遇到的问题是 ABPeoplePickerNavigationController 有一个取消按钮放置在 rightBarButtonItem 插槽中,我必须更新导航栏

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

我已经在我的博客上记录了整个过程,其中包含一个工作示例,该示例应该允许您创建类似于 iPhone 上的联系人样式应用程序。希望这可以帮助。

于 2011-10-15T11:09:33.160 回答
3

我发现 Scott Sherwood 的方法以及他在他的网站上发布的演示非常有帮助。不过,正如他博客上的一位评论者所提到的,“编辑”模式下的“取消”按钮存在问题。

我刚刚提出了对 Scott 演示的修复,以及 Person View Controller 的不同方法:http: //finalize.com/2013/05/12/using-and-customizing-the-address-book-ui/

我对 Person View Controller 的建议是在协议方法 peoplePickerNavigationController:shouldContinueAfterSelectingPerson: 中手动放置 ABPeoplePickerNavigationControllerDelegate。

// Displays the information of a selected person
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{

    ABPersonViewController *view = [[ABPersonViewController alloc] init];

    view.personViewDelegate = self;
    view.displayedPerson = person; // Assume person is already defined.
    view.allowsEditing = YES;
    view.allowsActions = YES;

    [peoplePicker pushViewController:view animated:YES];

    return NO;
}

这里唯一的问题是名称的人员选取器表视图在编辑后不会自动刷新。这可以通过使用通讯簿回调来解决。我在我发布的 GitHub 项目中展示了如何做到这一点:

https://github.com/scottcarter/AddressBookPeoplePicker.git

于 2013-05-12T16:09:04.947 回答
1

似乎无法直接从 ABPeoplePickerNavigationController 添加新联系人。因此,当用户单击添加按钮时,我将呈现一个带有两个按钮的 UIActionSheet:

- (void) addContact{

    contactMenu = [[UIActionSheet alloc] 
                           initWithTitle: nil 
                           delegate:self
                           cancelButtonTitle:@"Cancel"
                           destructiveButtonTitle: nil
                           otherButtonTitles:@"Select a contact", @"Add a new contact", NULL];

    [contactMenu showInView:self.view];

}

这是关联的委托方法:

    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{



        if(buttonIndex == 0){
            // select an existing contact   
            ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
            peoplePicker.peoplePickerDelegate = self;
            [self presentModalViewController:peoplePicker animated:YES];

        }

        if(buttonIndex == 1){

            // add a new contact
            ABNewPersonViewController *newPersonViewController = [[ABNewPersonViewController alloc] init];
            newPersonViewController.newPersonViewDelegate = self;

            UINavigationController *personNavController = [[UINavigationController alloc] initWithRootViewController:newPersonViewController];
            [self presentModalViewController:personNavController animated:YES];

            [personNavController release];
            [newPersonViewController release];

        }

            if(buttonIndex == 2){
        // cancel the operation
        [actionSheet dismissWithClickedButtonIndex:2 animated:YES];
    }


}
于 2009-06-25T10:39:32.453 回答