【问题标题】:How to keep peoplePickerNavigationController from automatically dismissing如何防止人员选择器导航控制器自动关闭
【发布时间】:2014-09-21 01:36:58
【问题描述】:

在 iOS 8 中,以下内容已被弃用:

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

现在我们应该使用:

-(void)peoplePickerNavigationController:didSelectPerson:

但是这种方法会在第一次选择后自动关闭人员选择器,而旧版本没有。我有一个例程,需要一个一个地记录用户选择的每个名称。我可以在每次选择后重新显示人员选择器,但它会从第一个名字开始联系人列表。

我希望我正确地解释了这一点。任何人都知道如何防止 peoplepickernavigationcontroller 在 iOS 8 中像以前在 ios7 中那样自动关闭?

【问题讨论】:

  • 你找到解决办法了吗?
  • @NikitaIvaniushchenko 在下面查看我的答案

标签: ios ipad ios8 peoplepicker


【解决方案1】:

在 ABPeoplePickerNavigationController 的文档中,查看 predicateForSelectionOfPerson 的注释。

// Optionally determines if a selected person should be returned to the app (predicate evaluates to TRUE),
// or if the selected person should be displayed (predicate evaluates to FALSE).
// If not set and -peoplePickerNavigationController:didSelectPerson: is implemented the selected person is returned to the app,
// or if not set and -peoplePickerNavigationController:didSelectPerson:identifier: is implemented the selected person is displayed.
//
@property(nonatomic,copy) NSPredicate *predicateForSelectionOfPerson NS_AVAILABLE_IOS(8_0);

所以你需要设置一个谓词FALSE,如果你想显示被选中的人。

    if ([picker respondsToSelector:@selector(setPredicateForSelectionOfPerson:)])
{
    picker.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:NO];
}

【讨论】:

  • 反正不一样。在此之前,我有机会将另一个视图控制器推送到同一个导航堆栈中,但现在 ABPeoplePickerNavigationController 会自动关闭。
【解决方案2】:

我找到了在选择属性后重新显示人员选择器的解决方案。

实现当一个人选择联系人属性时处理的委托方法(仅由 iOS 8 调用):对我来说,诀窍是先关闭选择器,然后立即在完成委托中调用我的“显示选择器”方法(是的,委托中的委托)。

// Dismisses the people picker and shows the application when users tap Cancel.
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {


    [self.picker dismissViewControllerAnimated:NO completion:^{
        NSLog(@"just dismissed the picker");
        [self showPeoplePickerController];
    }];
}

如果您希望人员选择器显示在上次停止的位置,请确保初始化一次。希望这会有所帮助

这是我的 showPeoplePickerController 方法

#pragma mark Show all contacts
// Called when users tap "Display Picker" in the application. Displays a list of contacts and allows users to select a contact from that list.
-(void)showPeoplePickerController

{
        picker.peoplePickerDelegate = self;
        picker.delegate = self;
        picker.visibleViewController.searchDisplayController.searchBar.delegate = self;

        [self presentViewController:picker animated:NO completion:nil];  
}

首先初始化选择器。请注意,联系人访问首先需要授权方法调用

picker = [[ABPeoplePickerNavigationController alloc] init];
//have self prompt first, then based off answer prompt them with internal address book stuff or now
if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
    // show picker UI if the user has granted access to their Contacts
    [self showPeoplePickerController];
}

注意事项:

  • 我之前在加载视图时启动了人员选择器。一次。
  • 在呈现和关闭控制器时将“动画”选项设置为 NO 有助于使过渡更加顺畅。

【讨论】:

  • 不太好用。也许我做错了什么。你是如何在视图加载中初始化选择器的?
  • @JackRomano 你的代码在什么地方或者哪里被破坏了?还有更多信息吗?
猜你喜欢
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 2011-09-17
  • 2015-05-10
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
相关资源
最近更新 更多