我的应用需要将自定义类的实例与iPhone的AddressBook中的联系人记录相关联。当我呈现 ABPeoplePickerNavigationController 并允许用户选择现有的联系人时,一切都很好。问题是没有明显的方法允许用户轻松添加联系人记录,如果他们正在寻找的联系人记录已经存在于他们的地址簿中。
人们如何以轻松的方式从 ABPeoplePickerNavigationController 转到 ABNewPersonViewController 对用户来说直观吗?
答案 0 :(得分: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{
我已使用blog在我的worked example上记录了整个过程,该过程应该允许您创建类似于iPhone上的联系人样式应用程序。希望这会有所帮助。
答案 1 :(得分: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:for 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项目中展示如何做到这一点:
答案 2 :(得分: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];
}
}