CASMK
CASMK

Reputation: 55

Issue while pushing ABNewPersonViewController

I am using AddressBookUI Framework for Adding contact, when I tried to pushing this view controller then cancel and done button not working properly, I don't want to present it

Here is my code

ABNewPersonViewController *abnpvc = [[ABNewPersonViewController alloc] init];
[abnpvc setNewPersonViewDelegate: self];
[self.navigationController pushViewController:abnpvc animated:YES];

I am also tried add as subview rather then pushing it but when I am adding as subview then it was not added

As per comment i have tried like

UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:abnpvc];
[self presentViewController:navigation animated:YES completion:nil];

Can anyone help me out why properly not working ?

Upvotes: 2

Views: 472

Answers (3)

Ashish Thummar
Ashish Thummar

Reputation: 431

Apple guideline(IMPORTANT) :: New-person view controllers must be used with a navigation controller in order to function properly. It is recommended that you present a new-person view controller modally.

Add Delegate

@interface ViewController () <ABNewPersonViewControllerDelegate>

Pushing the viewController

ABNewPersonViewController *abnpvc = [[ABNewPersonViewController alloc] init];
[abnpvc setNewPersonViewDelegate: self];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:abnpvc];
[self presentModalViewController:navController animated:YES];

And Now Add Delegate Method

#pragma mark ABNewPersonViewControllerDelegate methods

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

That will work fine.

Upvotes: 1

E-Riddie
E-Riddie

Reputation: 14780

You can implement that too considering also the other answers and the deprecations to ABNewPersonViewController in iOS 9.

As per your remarks:

cancel and done button not working properly

They are working if you have included the ABNewPersonViewControllerDelegate on interface like this:

@interface ViewController () <ABNewPersonViewControllerDelegate>

Pushing the viewController on navigation stack like this:

ABNewPersonViewController *controller = [[ABNewPersonViewController alloc] init];
controller.newPersonViewDelegate = self;
[self.navigationController pushViewController:controller animated:YES];

And by conforming to the protocol by implementing this method:

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(nullable ABRecordRef)person {
    // Trick to go back to your view by popping it from the navigation stack when done or cancel button is pressed
    [self.navigationController popViewControllerAnimated:YES];
}

The tricky line is to pop the newPersonController from the navigation stack when either Done or Cancel button are pressed.

Enjoy it

Upvotes: 2

Tobi Nary
Tobi Nary

Reputation: 4596

Why can't you just do it as the docs say?

It is recommended that you present a new-person view controller modally.

Use

ABNewPersonViewController *abnpvc = [[ABNewPersonViewController alloc] init];
[abnpvc setNewPersonViewDelegate: self];
[self presentViewController:abnpvc animated:YES completion:nil];

That should work fine.

Edit

On second thought, did you set your delegate correctly and do the implementations get called? I suspect they are not implemented or the delegate is not set correctly.

Upvotes: 1

Related Questions