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 UIViewContro
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:
#import "PeoplePickerViewControllerWrapper.h"
@implementation PeoplePickerViewControllerWrapper
@synthesize ppvc = _ppvc; // This is the object I'm proxying (The proxyee so to speak)
@synthesize delegate = _delegate;
- (void)awakeFromNib
{
self.ppvc = [[ABPeoplePickerNavigationController alloc] init ];
self.ppvc.peoplePickerDelegate = self;
self.ppvc.addressBook = ABAddressBookCreate();
self.ppvc.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
}
#pragma mark - View lifecycle
- (void)loadView
{
[super loadView];
[self.ppvc loadView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.ppvc viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.ppvc viewWillAppear:animated];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[self.ppvc viewDidDisappear:animated];
}
-(UIView *)view{
return self.ppvc.view;
}
- (void)viewDidUnload
{
[super viewDidUnload];
[self.ppvc viewDidUnload];
}
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:
- (void)awakeFromNib
{
ABPeoplePickerNavigationController * peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
// Display only a person's phone and address
NSArray * displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonAddressProperty],
[NSNumber numberWithInt:kABPersonPhoneProperty],
nil];
peoplePicker.displayedProperties = displayedItems;
[self.view addSubview:peoplePicker.view];
[self addChildViewController:peoplePicker];
[peoplePicker didMoveToParentViewController:self];
}
Besi's answer is great. But it's less code to just use the old way instead of using a storyboard for it:
- (void)showPeoplePicker:(id)sender
{
ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
picker.modalPresentationStyle = UIModalPresentationFullScreen;
picker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:picker
animated:YES
completion:^{
// animation to show view controller has completed.
}];
}