2

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 ?

CASMK
  • 55
  • 8

3 Answers3

2

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

E-Riddie
  • 14,660
  • 7
  • 52
  • 74
  • Thanks it works, but one more question i have filled all detail how can i get the detail that i have filled in form ? – CASMK Mar 01 '16 at 12:56
  • You have the person (nullable ABRecordRef) variable which holds your data – E-Riddie Mar 01 '16 at 12:57
  • when i tried to print that details it comes like `` this how can i getting object ? – CASMK Mar 01 '16 at 12:59
  • Please look [here](https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AddressBook/Tasks/AccessingData.html) how to access it's data as this is an encoded object which will log the reference – E-Riddie Mar 01 '16 at 13:02
1

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.

Tobi Nary
  • 4,566
  • 4
  • 30
  • 50
  • thanks for your answer i have updated my question i have already tried via presening view controllor not working buttons for me and also when i tried to run your code there is no any button cancel or done so we have to use navigationbar right ? – CASMK Mar 01 '16 at 12:36
1

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.

Ashish Thummar
  • 431
  • 4
  • 10
  • Thanks for your answer , I tried your code but problem is buttons are not working cancel or done – CASMK Mar 01 '16 at 12:41