How to get around the lack of an add button in ABPeoplePickerNavigationController?

My application needs to associate instances of a custom class with writing contacts in the iPhone AddressBook. All is well and good when I introduce ABPeoplePickerNavigationController and allows the user to select an existing contact. The problem is that there is no obvious way to allow the user to easily ADD a contact record if the one they are looking for does not yet exist in their address book.

How do people get from ABPeoplePickerNavigationController to ABNewPersonViewController in such a simple and intuitive way for the user?

+3
source share
3 answers

You can create a UIBarButton and add it to the UINavigationBar from an ABPeoplePickerNavigationController like this.

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

-(IBAction)addPerson:(id)sender{
    ABNewPersonViewController *view = [[ABNewPersonViewController alloc] init];
    view.newPersonViewDelegate = self;
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:view];
    [self.picker presentModalViewController:nc animated:YES];
}

The problem I ran into was that ABPeoplePavlerNavigationController has a cancel button placed in the rightBarButtonItem slot and I had to update the navigation bar to

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

I documented the entire process on my blog with a proven example that should allow you to create a contact style application similar to an iPhone application. Hope this helps.

+6
source

, , . , "" .

, Person View: http://finalize.com/2013/05/12/using-and-customizing-the-address-book-ui/

Person View Controller , peoplePickerNavigationController: shouldContinueAfterSelectingPerson: ABPeoplePickerNavigationControllerDelegate.

// Displays the information of a selected person
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{

    ABPersonViewController *view = [[ABPersonViewController alloc] init];

    view.personViewDelegate = self;
    view.displayedPerson = person; // Assume person is already defined.
    view.allowsEditing = YES;
    view.allowsActions = YES;

    [peoplePicker pushViewController:view animated:YES];

    return NO;
}

, . . , GitHub, :

https://github.com/scottcarter/AddressBookPeoplePicker.git

+3

, ABPeoplePickerNavigationController. , , UIActionSheet :

- (void) addContact{

    contactMenu = [[UIActionSheet alloc] 
                           initWithTitle: nil 
                           delegate:self
                           cancelButtonTitle:@"Cancel"
                           destructiveButtonTitle: nil
                           otherButtonTitles:@"Select a contact", @"Add a new contact", NULL];

    [contactMenu showInView:self.view];

}

:

    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{



        if(buttonIndex == 0){
            // select an existing contact   
            ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
            peoplePicker.peoplePickerDelegate = self;
            [self presentModalViewController:peoplePicker animated:YES];

        }

        if(buttonIndex == 1){

            // add a new contact
            ABNewPersonViewController *newPersonViewController = [[ABNewPersonViewController alloc] init];
            newPersonViewController.newPersonViewDelegate = self;

            UINavigationController *personNavController = [[UINavigationController alloc] initWithRootViewController:newPersonViewController];
            [self presentModalViewController:personNavController animated:YES];

            [personNavController release];
            [newPersonViewController release];

        }

            if(buttonIndex == 2){
        // cancel the operation
        [actionSheet dismissWithClickedButtonIndex:2 animated:YES];
    }


}
+1

Source: https://habr.com/ru/post/1711218/


All Articles