[ios]ABPeoplePickerNavigationController shouldContinueAfterSelectingPerson 返回搜索结果

发布时间: 2017/3/21 23:01:25
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person {

        DefaultContactSelectViewController *view = [[self storyboard] instantiateViewControllerWithIdentifier:@"DefaultContactView"];
        view.recordID  = recordID;
        view.phones = phones;
        view.emails = emails;
        view.first_name = first_name;
        view.last_name = last_name;
        view.delegate = self;

        [peoplePicker pushViewController:view animated:YES];
    return NO;
}

在上面的代码示例中,我推着一个自定义的联系人视图控制器选择一个联系人后。问题是如果联系人选择从搜索结果中,然后用户点击回去回到联系人选择器,搜索结果将被清除。

如果上面的代码返回是的但那么这将促使默认联系人视图中是不是我想要什么,就不会发生此问题。

提前谢谢,如果你知道如何可以修复此问题。

解决方法 1:

您可能应该编写自定义的 PeoplePickerViewController,因为你永远不会足够对苹果的默认控制器的控制。

不管怎么说,至于你当前的问题,这里是你需要做什么︰

声明三个新属性 (使用适当的声明的基础,如果您使用ARC或不 — — 我假设没有圆ARC)

@property (nonatomic, assign) ABPeoplePickerNavigationController *peoplePicker;
@property (nonatomic, assign) UIViewController *peoplePickerRootViewController;
@property (nonatomic, copy) NSString *currentSearchString;

现在,当您显示人员选取器,添加以下行︰

// save people picker when displaying
self.peoplePicker = [[[ABPeoplePickerNavigationController alloc] init] autorelease];

// save it's top view controller
self.peoplePickerRootViewController = self.peoplePicker.topViewController;

// need to see when view controller is shown/hidden - viewWillAppear:/viewWillDisappear: won't work so don't bother with it.
self.peoplePicker.delegate = self;

现在,我们会在推人视图之前只是保存搜索字符串︰

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    self.currentSearchString = nil;
    if ([self.peoplePickerRootViewController.searchDisplayController isActive])
    {
        self.currentSearchString = self.peoplePickerRootViewController.searchDisplayController.searchBar.text;
    }
    // other stuff... 

显然,在此类中实现 UINavigationControllerDelegate。当根视图回来到视图中时,我们将强制显示搜索结果视图。这里是执行navigationController:willShowViewController:animated:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (navigationController == self.peoplePicker)
    {
        if (viewController == self.peoplePickerRootViewController)
        {
            if (self.currentSearchString)
            {
                [self.peoplePickerRootViewController.searchDisplayController setActive: YES];
                self.peoplePickerRootViewController.searchDisplayController.searchBar.text = self.currentSearchString;
                [self.peoplePickerRootViewController.searchDisplayController.searchBar becomeFirstResponder];
            }
            self.currentSearchString = nil;
        }
    }
}

别忘了释放 currentSearchString 在 dealloc 中的,如果不使用ARC。

一个小小警告︰ 当你选择一个人,ABPeoplePickerNavigationController 试图隐藏搜索结果视图时有轻微的闪烁。

赞助商