0

我有一个 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 按钮将电话号码添加到 telField1 contact2 按钮将电话号码添加到 telField2 contact3 按钮将电话号码添加到 telField3

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

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

4

1 回答 1

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;

}

它将通过单击按钮在文本字段中显示联系人,无需为每个文本字段设置单独的按钮。

于 2012-10-25T05:47:58.283 回答