将ABPersonViewController推送到iOS8上的ABPeoplePickerNavigationController

时间:2014-10-30 20:09:05

标签: ios objective-c ios8 abpeoplepickerview abpersonviewcontroller

介绍人员选择器

ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.allowsActions = YES;
peoplePicker.allowsEditing = NO;
peoplePicker.peoplePickerDelegate = self;
[self presentViewController:peoplePicker animated:YES completion:nil];

在iOS 7中实现ABPeoplePickerNavigationControllerDelegate

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
  ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
  personViewController.displayedPerson = person;
  [peoplePicker pushViewController:personViewController animated:YES];
  return NO;
}

到目前为止一切顺利。人物视图控制器按预期显示。 iOS7方法返回一个值 - 可以返回 NO 以确保人员选择器保持打开状态。在iOS8中,不推荐使用上述委托方法,并且必须实现新方法:

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
  ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
  personViewController.displayedPerson = person;
  [peoplePicker pushViewController:personViewController animated:YES];
}

人员视图控制器被推送到人员选择器,但在几分之一秒后,人员选择器被解雇(与人员视图控制器一起)。

有没有办法防止人们选择器在iOS8上被解雇?还有其他建议吗?

3 个答案:

答案 0 :(得分:0)

-(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];
    }];
}

答案 1 :(得分:0)

如何在Xcode 6中使用presentViewController代替pushViewController ...它说pushViewController已被弃用,我看到你正试图在iOS8中实现它,所以试一试... 像这一行:

[peoplePicker presentViewController: personViewController animated: YES completion: nil];

答案 2 :(得分:0)

在iOS8中,初始化ABPeoplePickerNavigationController时需要添加如下代码,否则在选择联系人后,peoplePickerNavigationController将立即解除。

if(IOS8_OR_LATER){
    peoplePicker.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false]; }