无法使用 ABNewPersonViewController 在通讯录中显示

发布于 2024-12-20 02:39:14 字数 2409 浏览 1 评论 0原文

我对 Objective 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];
}

写入,当我单击“执行”按钮时,我得到一个黑屏:这就是我得到的

在此处输入图像描述

它这样做有原因吗?

多谢!

编辑

当前代码:

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

}

仍然是同样的问题

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2024-12-27 02:39:14

好吧,你的问题是这里的这一行:

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

如果你用以下内容替换该行:

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

你的代码工作正常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文