Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display ABPeoplePickerNavigationController using storyboard segue

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.

like image 853
Besi Avatar asked Jan 21 '12 12:01

Besi


1 Answers

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.
                     }];
}
like image 114
Matt Connolly Avatar answered Sep 21 '22 06:09

Matt Connolly