将联系电话号码添加到多个uitextfiels

时间:2012-10-25 05:45:08

标签: iphone uitextview abaddressbook

我有一个uibutton,当点击时会显示联系人列表并将所选的电话号码添加到uitextfield。这是我正在使用的代码。

- (IBAction)contact1:(id)sender
{
ABPeoplePickerNavigationController *picker1 =
[[ABPeoplePickerNavigationController alloc] init];
picker1.peoplePickerDelegate = self;
[self presentModalViewController:picker1 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* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
    phone = (__bridge_transfer NSString*)
    ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

 } else {
    phone = @"[None]";
    }
 self.telField1.text = phone;
 }

我要做的是让多个uibuttons将电话号码添加到多个uitextfield。

例如:contact1按钮将phone#添加到telField1                contact2按钮将phone#添加到telField2                contact3按钮将phone#添加到telField3

每个文本字段都有不同的电话#。

我的原始代码可以是tweeked还是我应该尝试另一种方法?

1 个答案:

答案 0 :(得分:1)

-(IBAction)getContact {
// creating the picker
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
// place the delegate of the picker to the controll
picker.peoplePickerDelegate = self;

// showing the picker
[self presentModalViewController:picker animated:YES];
// releasing
[picker release];

}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
// assigning control back to the main controller
[self dismissModalViewControllerAnimated:YES];

}

  - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

// setting the first name
firstName.text = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

// setting the last name
lastName.text = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);   

ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
number.text = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);

// remove the controller
[self dismissModalViewControllerAnimated:YES];

return NO;

}

  - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
return NO;

}

只需单击按钮即可在文本字段中显示联系人,无需为每个文本字段设置单独的按钮。