我按照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;
}
答案 0 :(得分: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;
然后第一个“+”按钮会有一个IBAction
,如:
- (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指定您要更新的电话号码。