gpt4 book ai didi

iOS通讯录: Cannot setup an ABNewPersonViewController in storyboard

转载 作者:行者123 更新时间:2023-12-01 19:09:31 25 4
gpt4 key购买 nike

我有一个 ABNewPersonViewController 的子类根据文档的要求,在 Storyboard中定义,并嵌入到导航 Controller 中。导航 Controller 本身由标签栏 Controller 控制。在身份检查器中,类已设置为子类。
enter image description here
在子类 ABNewPersonViewController我在 viewDidLoad 中进行测试初始化:

- (void)viewDidLoad
{
[super viewDidLoad];
self.newPersonViewDelegate = self;

ABRecordRef newPerson= ABPersonCreate();
CFErrorRef error = NULL;
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, CFSTR("First"), &error);
ABRecordSetValue(newPerson, kABPersonLastNameProperty, CFSTR("Last"), &error);
assert(!error);
[self setDisplayedPerson:newPerson];
}

在我的子类中,我还实现了委托(delegate)方法 newPersonViewController:didCompleteWithNewPerson: ,但这没关系。
当我在标签栏 Controller 中选择标签时,会显示黑屏。
知道我做错了什么吗?
PS:我知道如何以编程方式进行设置,但我想在 Storyboard 中进行。

编辑

当我以编程方式执行此操作时,子类由 alloc 初始化& init .这可以正常工作。当子类从 Storyboard 中实例化时,它会收到 initWithCoder: .为了测试它,我实现了 initWithCoder:通过以下方式:
- (id)initWithCoder:(NSCoder*)coder{  
self = [super init];
return self;
}

在这种情况下, ABNewPersonViewController 的入口掩码确实显示了,但是导航栏没有显示为空。我知道这个初始化技巧是错误的,但是有人知道如何正确地做吗?

编辑

导航栏未显示,因为我将其设置为隐藏。通常会显示,但缺少“完成”和“取消”按钮。这当然不足为奇,因为父类(super class)不是由 initWithCoder: 初始化的。但由 init .
问题仍然是为什么不显示输入掩码,而只显示黑屏。

最佳答案

使用以下代码设置 ABNewPersonViewController,它对我来说很好:

  ABRecordRef newPerson= ABPersonCreate();
CFErrorRef error = NULL;
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, CFSTR("Fname"), &error);
ABRecordSetValue(newPerson, kABPersonLastNameProperty, CFSTR("Lanme"), &error);
NSAssert(!error, @"something bad happend here.");


ABNewPersonViewController* newPersonViewController = [[ABNewPersonViewController alloc] init];
[newPersonViewController setDisplayedPerson:newPerson];
[newPersonViewController setNewPersonViewDelegate:self];


[self presentModalViewController:newPersonViewController animated:YES];

CFRelease(newPerson);

- - - - - - - 或者 - - - - -
ABUnknownPersonViewController *picker = [[ABUnknownPersonViewController alloc] init];
// picker.unknownPersonViewDelegate = self;
picker.displayedPerson = aContact;
picker.allowsAddingToAddressBook = YES;
picker.allowsActions = YES;
picker.alternateName = @"";
picker.title = @"Add to Contacts";
picker.message = @"";
[picker setHidesBottomBarWhenPushed:YES];
[self.navigationController pushViewController:picker animated:YES];

或者要添加新联系人或添加到现有联系人,您可以使用以下之一:
ABRecordRef aContact = ABPersonCreate();
CFErrorRef anError = NULL;
// store mobile number
ABMultiValueRef mobilenumber = ABMultiValueCreateMutable(kABMultiStringPropertyType);
bool didAdd = ABMultiValueAddValueAndLabel(mobilenumber, (__bridge CFTypeRef)(number), kABPersonPhoneMobileLabel, NULL);

//store email address
NSString *strEmail = [NSString stringWithFormat:@"%@",number];
strEmail = [strEmail stringByReplacingOccurrencesOfString:@"mailto:" withString:@""];

ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
bool didAddEmail = ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(strEmail), kABOtherLabel, NULL);


if (didAdd == YES || didAddEmail== YES) {
ABRecordSetValue(aContact, kABPersonEmailProperty, mobilenumber, &anError);
ABRecordSetValue(aContact, kABPersonEmailProperty, emailMultiValue, &anError);

if (anError == NULL) {
ABUnknownPersonViewController *picker = [[ABUnknownPersonViewController alloc] init];
// picker.unknownPersonViewDelegate = self;
picker.displayedPerson = aContact;
picker.allowsAddingToAddressBook = YES;
picker.allowsActions = YES;
picker.alternateName = @"";
picker.title = @"Add to Contacts";
picker.message = @"";
[picker setHidesBottomBarWhenPushed:YES];
[self.navigationController pushViewController:picker animated:YES];

// UINavigationController *newNavigationController = [[UINavigationController alloc] initWithRootViewController:picker];
//
// [self presentViewController:newNavigationController animated:YES completion:nil];
//
picker = nil;
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Could not create unknown user"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert show];
alert = nil;
}
}
CFRelease(mobilenumber);
CFRelease(aContact);

关于iOS通讯录: Cannot setup an ABNewPersonViewController in storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17477569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com