ABPeoplePickerNavigationController应该继续选择后返回搜索结果

时间:2013-04-11 01:19:27

标签: ios objective-c uinavigationcontroller addressbookui

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

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

如果上面的代码返回YES,则不会发生此问题,但是它会推送默认的联系人视图,这不是我想要的。

如果您知道如何解决此问题,请提前致谢。

2 个答案:

答案 0 :(得分:2)

您应该编写一个自定义的PeoplePickerViewController,因为您永远无法控制Apple的默认控制器。

无论如何,至于你目前的问题,这就是你需要做的事情:

声明三个新属性(根据你是否使用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;
        }
    }
}

如果不使用ARC,请不要忘记在dealloc中释放currentSearchString。

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

答案 1 :(得分:0)

好的我有类似的问题。我猜你正在使用ARC?

如果是这样我保存并将整个传递给ABRecordRef到我的其他视图,然后使用以下方法保留person对象:

CFRetain( m_MyContact object );

完成后不要忘记在对象上使用CFRelease()。