无法使用ABNewPersonViewController在地址簿中显示

时间:2011-12-07 20:35:27

标签: objective-c ios xcode4.2 addressbook abpersonviewcontroller

我对目标C很新,我正在使用XCode 4.2,在应用程序的一部分中,我需要将数据保存在地址簿中。但我需要将它显示在地址簿中,而不是仅将其保存在按钮上

我正在尝试使用以下代码:

我添加了addressBook框架和addressBookUI框架 在我写的.h文件中

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


    @interface HelloWorld20ViewController : UIViewController<ABNewPersonViewControllerDelegate,ABPersonViewControllerDelegate>{

}
-(IBAction)go;

@end

在我写的.m文件中

-(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];
}

当我点击go按钮时,我得到一个黑屏:这是我得到的

enter image description here

这是为什么会这样做的?

非常感谢!

被修改

当前代码:

-(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);

}

仍然是同样的问题

1 个答案:

答案 0 :(得分:1)

好的,你的问题就在这里:

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

如果用这个替换该行:

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

您的代码运行正常。