您可能应该编写一个自定义的 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 试图隐藏搜索结果视图时,当您选择一个人时会有轻微的闪烁。