3

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"...

Kann
  • 375
  • 1
  • 6
  • 22
  • For what it's worth, Apple's own [QuickContacts](http://developer.apple.com/library/ios/#samplecode/QuickContacts/Introduction/Intro.html) example (that's linked to in the Address Book Programming Guide for iOS) has the same "Cancel" button. Kudos for figuring it out. – John Sauer Jan 10 '13 at 14:51

3 Answers3

2

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)];

}
Tom Andersen
  • 6,788
  • 2
  • 34
  • 53
  • This is correct. Somehow, I need to made this modification for my iOS7 app as well. Thanks for the tip. – Kann Jan 06 '14 at 10:40
  • Instead of adding a delay, you could use the completion block in your presentViewController method call. Also, why not put the button on the left and allow the user to cancel with the default button on the right? – spstanley May 09 '14 at 20:00
0

Never mind, I got it to work now. It seems that I need to present the ViewController before overriding its button...


- (void)showPicker
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
    [picker.topViewController setEdit:YES];
    picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];
}
Kann
  • 375
  • 1
  • 6
  • 22
-1

You just need a small modification in the addPerson method.

For displaying contacts:

-(void)showPicker

{

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController       alloc] init];
  picker.peoplePickerDelegate = self;
  [self presentViewController:picker animated:YES completion:nil];
  [picker.topViewController setEdit:YES];
  [self performSelector:@selector(addContact:) withObject:picker afterDelay:0.1];

}

-(void)addContact:(ABPeoplePickerNavigationController*)picker;

{

picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];

}

By this code you will get a plus sign in the right top corner to add the contacts.

We are using @selector for this.

-(void)addPerson

{

ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController 
alloc] init];
newPersonVC.newPersonViewDelegate = self;
UINavigationController *nc = [[UINavigationController alloc]     initWithRootViewController:newPersonVC];

//Here is the code of line that do the work.By using self.presentedViewController we can //get the add contact screen otherwise we will get the error like empty view hierarchy.

    [self.presentedViewController presentViewController:nc animated:YES completion:nil];

}

Hiren
  • 648
  • 7
  • 19