I have a new project where I want to display a People Picker, when a button is touched.
So I have a UIButton
that segues to a generic UIViewController
with the identifier showContacts
. I set the class of this ViewController to ABPeoplePickerNavigationController
.
Now in my root ViewController I have this code to initialize my picker:
#pragma mark - Segues
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showContacts"]){
ABPeoplePickerNavigationController *ppnc = segue.destinationViewController;
ppnc.peoplePickerDelegate = self;
ppnc.addressBook = ABAddressBookCreate();
ppnc.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
}
}
Although I have added test contacts to my Simulator Address book the results looks like this:
no picker http://i.minus.com/jbwUQyLr36ChHo.png
With the following code, which is very similar to what I do in the prepareForSegue:
method, I manage to show a picker via an IBAction
:
- (IBAction)showPicker:(id)sender {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonBirthdayProperty], nil];
picker.displayedProperties = displayedItems;
// Show the picker
[self presentModalViewController:picker animated:YES];
}
The result:
picker http://i.minus.com/jeEVeIBmfIYdR.png
It is not clear to me why the People picker does not show.
Besi's answer is great. But it's less code to just use the old way instead of using a storyboard for it:
As Matt noted the good old way is ok. If you want to use storyboard you can add ABPeoplePickerNavigationController in your customized view controller like this:
Curiously what I suggested on Can I use segues with designated initializers of view controllers? fixed the issue for me as well. So creating a proxy ViewController for the
ABPeoplePickerNavigationController
fixes the issue, but it does not explain, why the built-in pickers can't be used in storyboards:This is the code for my wrapper class: