ABPeoplePickerNavigationController shouldContinueAfterSelectingPerson returns a search result

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

In the above code example, I select the user controller of the contact view after selecting the contact. The problem is that the contact is selected from the search result, and then the user clicks the button to return to the contact selection item, the search result will be deleted.

this problem does not occur if the above code returns YES, but then it will call the default view, which is not what I want.

Thanks in advance if you know how I can fix this problem.

+4
source share
2 answers

You should probably write your own PeoplePickerViewController since you will never have enough control over the default Apple controller.

In any case, regarding your current problem, here is what you need to do:

Declare three new properties (use appropriate declarations based on if you use ARC or not - I do not assume ARC)

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

Now that you are showing the people picker, add the following lines:

 // save people picker when displaying self.peoplePicker = [[[ABPeoplePickerNavigationController alloc] init] autorelease]; // save it 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; 

Now we will save the search bar just before clicking on the user icon:

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

Obviously, implement the UINavigationControllerDelegate in this class. When the root view returns to view, we will force the search results to be displayed. Here's the implementation for 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; } } } 

Remember to release currentSearchString in dealloc if you are not using ARC.

A little caution. When choosing a person, when ABPeoplePickerNavigationController tries to hide the presentation of the search results, a slight flicker appears.

+2
source

I had a similar problem. I suppose you use ARC?

If I saved and passed an integer in ABRecordRef to another view of mine, then I had to save the person object using:

 CFRetain( m_MyContact object ); 

Remember to use CFRelease () for the object when you are done.

0
source

All Articles