无法使用 ABNewPersonViewController 在通讯录中显示
我对 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,你的问题是这里的这一行:
如果你用以下内容替换该行:
你的代码工作正常。
Alright, your problem is this line here:
If you replace that line with this:
Your code works fine.