0

我按照 Apple Address Book Programming Guide for iOS将电话号码从地址簿获取到我的UITextField. 但我有两个UITextField,不知道如何对第二个文本字段实现相同的功能。

呈现人员选择器

- (IBAction)showPicker:(id)sender

{

    ABPeoplePickerNavigationController *picker =

            [[ABPeoplePickerNavigationController alloc] init];

    picker.peoplePickerDelegate = self;



    [self presentModalViewController:picker animated:YES];

}

响应人员选择器中的用户操作

- (void)peoplePickerNavigationControllerDidCancel:

            (ABPeoplePickerNavigationController *)peoplePicker

{

    [self dismissModalViewControllerAnimated:YES];

}





- (BOOL)peoplePickerNavigationController:

            (ABPeoplePickerNavigationController *)peoplePicker

      shouldContinueAfterSelectingPerson:(ABRecordRef)person {



    [self displayPerson:person];

    [self dismissModalViewControllerAnimated:YES];



    return NO;

}



- (BOOL)peoplePickerNavigationController:

            (ABPeoplePickerNavigationController *)peoplePicker

      shouldContinueAfterSelectingPerson:(ABRecordRef)person

                                property:(ABPropertyID)property

                              identifier:(ABMultiValueIdentifier)identifier

{

    return NO;

}

显示一个人的信息

- (void)displayPerson:(ABRecordRef)person

{

    NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,

                                               kABPersonFirstNameProperty);

    self.firstName.text = name;



    NSString* phone = nil;

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,

                                    kABPersonPhoneProperty);

    if (ABMultiValueGetCount(phoneNumbers) > 0) {

       phone = (__bridge_transfer NSString*)

               ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

    } else {

        phone = @"[None]";

    }

    self.phoneNumber.text = phone;

}

编辑

- (void)displayPerson:(ABRecordRef)person

{

    NSString* phone = nil;

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    if (ABMultiValueGetCount(phoneNumbers) > 0) {

        phone = (__bridge_transfer NSString*)

        ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

    } else {

        phone = @"None";

    }


    self.phoneNumber.text = phone;



    if (ABMultiValueGetCount(phoneNumbers) > 1) {

        phone = (__bridge_transfer NSString*)

        ABMultiValueCopyValueAtIndex(phoneNumbers, 1);

    } else {

        phone = @"None";

    }

    self.phoneNumber2.text = phone;


}

在此处输入图像描述

4

1 回答 1

1

我假设你的第二个UITextField是第二个电话号码,如果它存在的话?然后你可以这样做:

if (ABMultiValueGetCount(phoneNumbers) > 1) {
   phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 1);
} else {
    phone = @"[None]";
}

self.phoneNumber2.text = phone;

或者您可以使用CFBridgingRelease而不是__bridge_transfer

phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, 1));

而且,顺便说一句,您是否还想用它ABMultiValueCopyLabelAtIndex来捕获每个电话号码的类型(如果您不知道电话号码是什么类型,那么知道电话号码是什么也无济于事)。您可以将这两个电话号码标签存储在两个额外的 UITextField 中(大概在它们各自的电话号码旁边)。


更新:

我最初以为您正在寻找同一个联系人的两个电话号码。我现在从您的用户界面推断,在您想要点击按钮的两个电话号码文本字段中的每一个旁边都有一个“+”按钮,然后拉出该文本字段的电话号码。然后你点击另一个文本字段旁边的“+”按钮,然后拉出另一个联系人的电话号码并将其放在该文本字段中。

因此,如果是这种情况,您可以在类中添加一个 ivar,例如:

int _phoneNumberIndex;

然后第一个“+”按钮会有一个IBActionlike:

- (IBAction)showPicker1:(id)sender
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];

    picker.peoplePickerDelegate = self;

    _phoneNumberIndex = 1;

    [self presentModalViewController:picker animated:YES];
}

而第二个“+”按钮会有IBAction类似的效果:

- (IBAction)showPicker2:(id)sender
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];

    picker.peoplePickerDelegate = self;

    _phoneNumberIndex = 2;

    [self presentModalViewController:picker animated:YES];
}

然后你displayPerson会是这样的:

- (void)displayPerson:(ABRecordRef)person
{
    // first name

    NSString* name = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));

    self.firstName.text = name;

    // phone numbers

    NSString* phone;

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    // first phone number

    if (ABMultiValueGetCount(phoneNumbers) > 0)
        phone = CFBridingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, 0));
    else
        phone = @"[None]";

    if (_phoneNumberIndex == 1)
        self.phoneNumber.text = phone;
    else if (_phoneNumberIndex == 2)
        self.phoneNumber2.text = phone;
    else
        NSLog(@"Unrecognized _phoneNumberIndex");

    CFRelease(phoneNumbers);
}

顺便说一句,虽然我认为这可以满足您的需求,但尚不清楚您应该对名字字段做什么(因为您要选择两个联系人,您想使用哪个名字,或者您愿意将两个名字存储在两个字段中,就像您使用电话号码一样?)。所以,你必须看看那个。

显然,如果您不想要两个IBAction“+”按钮的两种方法,您可能会做一个查看sender或它的标签的方法,但这对您的问题并不重要。最重要的是,使用 ivar 指定要更新的电话号码。

于 2012-05-28T03:13:03.320 回答