0

I am quite new to objective C , I am using XCode 4.2 and in a part of the application I will need to save data in the address book . but I need it to be displayed in the address book and not just save it on a button click

I am trying to use the following code :

I added the addressBook framework and the addressBookUI framework in my .h file I wrote

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


    @interface HelloWorld20ViewController : UIViewController<ABNewPersonViewControllerDelegate,ABPersonViewControllerDelegate>{

}
-(IBAction)go;

@end

in my .m file i wrote

-(IBAction)go{
    ABRecordRef newPerson= ABPersonCreate();
    CFErrorRef error = NULL;
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, CFSTR("john"), &error);
    ABRecordSetValue(newPerson, kABPersonLastNameProperty, CFSTR("Smith"), &error);
    NSAssert(!error, @"something bad happend here.");


    ABNewPersonViewController* newPersonViewController = [[ABNewPersonViewController alloc] initWithNibName:nil bundle:nil];
    [newPersonViewController setDisplayedPerson:newPerson];
    [newPersonViewController setNewPersonViewDelegate:self];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:newPersonViewController];

    [self presentModalViewController:navController animated:YES];

    CFRelease(newPerson);

}

-(void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person{
    [self.navigationController popViewControllerAnimated:YES];
}

when i click on the go button I get a black screen : here is what I am getting

enter image description here

is there a reason why it is doing this ?

Thanks alot!

Edited

current code :

-(IBAction)go{
    ABRecordRef newPerson= ABPersonCreate();
    CFErrorRef error = NULL;
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, CFSTR("john"), &error);
    ABRecordSetValue(newPerson, kABPersonLastNameProperty, CFSTR("Smith"), &error);
    NSAssert(!error, @"something bad happend here.");


    ABNewPersonViewController* newPersonViewController = [[ABNewPersonViewController alloc] initWithNibName:nil bundle:nil];
    [newPersonViewController setDisplayedPerson:newPerson];
    [newPersonViewController setNewPersonViewDelegate:self];


    [self presentModalViewController:newPersonViewController animated:YES];

    CFRelease(newPerson);

}

and still the same problem

user1051935
  • 579
  • 1
  • 10
  • 29
  • Why are you creating a new Navigation Controller? Just pass your address book controller, and it will work fine. – Richard J. Ross III Dec 07 '11 at 20:37
  • I removed the navigation controller from the IBAction function , I am still receiving a black screen , this time without the navigation bar ... so still not working – user1051935 Dec 07 '11 at 20:53

1 Answers1

1

Alright, your problem is this line here:

ABNewPersonViewController* newPersonViewController = [[ABNewPersonViewController alloc] initWithNibName:nil bundle:nil];

If you replace that line with this:

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

Your code works fine.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201