您應該編寫一個自定義的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嘗試隱藏搜索結果視圖時,您選擇一個人時會有輕微閃爍。
這工作,謝謝。我也保存了搜索結果的滾動位置,並添加:[self.peoplePickerRootViewController.searchDisplayController.searchResultsTableView setContentOffset:scrollPosition animated:NO]; tonavigationController「willShowViewController」,但設置滾動似乎只能在「didShowViewController」中工作,這並不理想。任何想法爲什麼它不會在willShowViewController中設置滾動? – 2013-05-07 02:41:34