iosaddressbookaddressbookui

iOS 6 Create new contact via ABPeoplePickerNavigationController


I am trying to follow Apple's Address book programming guide, and have question about peoplePickerController. The example photo from Apple tutorial has 'plus' button on the top-right which seems to be able to allow user to add new contact via peoplePickerController.

I called ABPeopleNavigationControllerPicker via


- (void)showPicker
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;

    [self presentViewController:picker animated:YES completion:nil];
}

But there's a cancel button on the top-right of the screen instead of the plus button. Is there any option to change the button or allow user to add new contact via this view controller?

After doing google search for quite a while, below is what I found. So I tried to programmatically change the bar button...


- (void)addPerson
{
    ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init];
    newPersonVC.newPersonViewDelegate = self;
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:newPersonVC];
    [self presentViewController:nc animated:YES completion:nil];
}
- (void)showPicker
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

However, somehow the bar button is still fixed as "cancel" instead of "add"...


Solution

  • On iOS 7 i needed to set up those buttons later...

     - (void)showPicker
     {
         ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
          picker.peoplePickerDelegate = self;
         [self presentViewController:picker animated:YES completion:nil];
       [picker.topViewController setEdit:YES];
       [self performSelector:@selector(setThemBuddons:) withObject:picker afterDelay:0.1];
    }
    
    -(void)setThemBuddons:(ABPeoplePickerNavigationController*)picker;
    {
         picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];
    
    }