views:

241

answers:

1

Hi guys I'm halfway through implementing a very basic 'Add Contact' Button. I am calling the 'Add View' using the code (via a linked UIButton, that works) :

- (IBAction)showAddContact {
 NSLog(@"Hit showAddContact");

 ABNewPersonViewController *newPersonViewController = [[ABNewPersonViewController alloc] init];

 addContactNavController = [[UINavigationController alloc] initWithRootViewController:newPersonViewController];
 [self presentModalViewController:addContactNavController animated:YES];
}

and then I have also set the delegate resonse of:

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person {
  NSLog(@"Hit newPersonViewController");
 //ABContact *contact = [ABContact contactWithRecord:person]; 
 [self.navigationController popViewControllerAnimated:YES];
}

in my header I have set:

#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

@interface test2ViewController : UIViewController <ABNewPersonViewControllerDelegate> {
UINavigationController* addContactNavController;
}

- (IBAction)showAddContact;

@end

I have added the frameworks Addressbook and AddressBookUI.

The add dialog box comes up as expected, I can edit the contact, but I am not able to remove the modal View Controller from the view.

I have even duplicated the problem in a simple test project available here:link text

What am I missing?, I bet it is something extremely simple. Thanks @norskben

+2  A: 

Couple of problems:

You should release the ABNewPersonViewController after presenting it.

You present the ABNewPersonViewController as a modal dialog with presentModalViewController: but you remove it from the screen as if it was pushed on a UINavigationController with popViewControllerAnimated:. Instead you should either push and pop, or present and dismiss. (If you don't know what this means, read a little int he View Controller documentation)

Even though you implement the ABNewPersonViewControllerDelegate protocol, you never set the delegate property of the ABNewPersonViewController.

St3fan
Thanks for the help. I was trying .delegate = self, and it wasnt loading, but on using newPersonViewController.newPersonViewDelegate = self; it worked as expected! Cheers. To all future browsers with the same question heres an example for you: http://norskben.posterous.com/iphoneos-addressbook-create-new-client-and-ge
norskben