iOS地址簿:无法在故事板中设置ABNewPersonViewController

时间:2013-07-04 20:13:38

标签: ios storyboard abaddressbook

我在故事板中定义了ABNewPersonViewController的子类,并根据文档的要求嵌入到导航控制器中。导航控制器本身由标签栏控制器控制。在身份检查器中,该类已设置为子类 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];
}  

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

编辑

当我以编程方式执行时,子类由alloc&初始化。 init。这工作正常。从storyboard实例化子类时,它会收到initWithCoder:。为了测试它,我通过以下方式实现了initWithCoder:

- (id)initWithCoder:(NSCoder*)coder{  
    self = [super init];  
    return self; 
}

在这种情况下,确实会显示ABNewPersonViewController的条目掩码,但导航栏未显示为空。我知道这个初始化hack是错误的,但有人知道怎么做对吗?

编辑

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

2 个答案:

答案 0 :(得分:2)

使用以下代码设置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);

答案 1 :(得分:1)

尝试这样做,在故事板中TabBarController的连接检查器中,有一个头部'Triggered Segues',从'view controllers'连接到'ABNewPersonViewController'。运行你的应用程序,它会工作。

如果您需要进一步导航,比如从ABNewPersonViewController到ChildOfABNewPersonViewController,那么您需要将ABNewPersonViewController连接到NavigationController和NavigationController的rootviewcontroller到ChildOfABNewPersonViewController。

干杯!!