7

I'd like to figure out a way so that, if the user presses the "Cancel" button (which I don't believe can be removed) in an ABPeoplePickerNavigationController, the view controller either doesn't close, or will be automatically reopened.

For example, given the following:

var picker = ABPeoplePickerNavigationController()
picker.peoplePickerDelegate = self
self.presentViewController(picker, animated: true, completion: nil)

I'd like to be able to do something like:

if (self.presentedViewController != picker && !userContinuedPastPicker) {
//where userContinuedPastPicker is a boolean set to false 
//in a delegate method called when the user clicks on an a contact 
//(meaning the user didn't press the cancel button but instead clicked on a contact)

    //create and present a UIAlertAction informing the user they must select a contact

    //present picker again
    self.presentViewController(picker, animated: true, completion: nil) 
}

This doesn't work; however, because the if statement won't "wait" until the user has pressed the cancel button or pressed a contact.

Randoms
  • 1,980
  • 2
  • 18
  • 29

1 Answers1

4

I'm not sure there is a way to remove the cancel button, or prevent it from working, but you could respond to the func peoplePickerNavigationControllerDidCancel(_ peoplePicker: ABPeoplePickerNavigationController!) delegate to handle the case where the cancel button is pressed.

I would recommend rather than immediately reopening the picker, you open an alert telling the user they need to pick someone, then open it back up from there. It may feel broken if they cancel and it immediately opens back up.

Reference

edit:
Presenting an alert or the picker probably needs to be delayed long enough for the previous picker to close. dispatch_after

Community
  • 1
  • 1
esthepiking
  • 1,619
  • 1
  • 18
  • 25
  • This seems to be almost exactly what I need! However, when I try to present another instance of an `ABPPNC` or a `UIAlertController` (as you so wisely suggested), I get an error saying: `Warning: Attempt to present on whose view is not in the window hierarchy!` – Randoms Aug 25 '15 at 01:13
  • My guess is you are presenting the alert while the people picker is animating off the screen. Try wrapping the presentViewController for the alert in a dispatch_after http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift – esthepiking Aug 25 '15 at 22:27