Ios 如何告诉通讯簿选择器要填写哪些字段?
我对iOS开发非常陌生。因此,请尽可能做出准确和基本的回答 我有一个表格,有两个(目前可能还有更多)字段,每个字段旁边都有一个按钮,允许用户从iPad通讯簿中选择联系人,并用通讯簿中的名字和姓氏填写相关字段 让我可以填写联系人姓名。但是,我希望能够单击“引用人”旁边的“浏览联系人”按钮,并让它使用相同的功能来填写“引用人”名称。我看到getContactName函数有sender参数。因此,我可以很容易地分辨出这两个按钮中的哪一个(或以后的其他按钮)被点击了 但是,当我从通讯簿中选择时,如何知道peoplePickerNavigationController或fillContactName函数中单击了哪个按钮Ios 如何告诉通讯簿选择器要填写哪些字段?,ios,xcode,xcode4,ios5,Ios,Xcode,Xcode4,Ios5,我对iOS开发非常陌生。因此,请尽可能做出准确和基本的回答 我有一个表格,有两个(目前可能还有更多)字段,每个字段旁边都有一个按钮,允许用户从iPad通讯簿中选择联系人,并用通讯簿中的名字和姓氏填写相关字段 让我可以填写联系人姓名。但是,我希望能够单击“引用人”旁边的“浏览联系人”按钮,并让它使用相同的功能来填写“引用人”名称。我看到getContactName函数有sender参数。因此,我可以很容易地分辨出这两个按钮中的哪一个(或以后的其他按钮)被点击了 但是,当我从通讯簿中选择时,如何知
- (IBAction)getContactName:(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 fillContactName:person];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
- (void)fillContactName:(ABRecordRef)person
{
NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
NSString *test = [firstName stringByAppendingString:@" "];
NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonLastNameProperty);
NSString *fullName = [test stringByAppendingString:lastName];
self.contactName.text = fullName;
}
这里有一种方法你可以做到 首先,在创建“浏览联系人”按钮时(或在情节提要中,如果使用情节提要),为其
标记
属性指定唯一的int值,以便在getContactName:
方法中区分它们。我建议第一个按钮设置tag=0,第二个按钮设置tag=1
然后向ViewController类添加一个新属性,该属性将在单击按钮后存储指向目标文本字段的指针。(请确保在.m文件中@synthesis)
在getContactName:
中,检查发送者对象并使用其标记
值适当设置指针。(请注意,发件人将是用户单击的UIButton对象)
然后在fillContactName:
中,将文本值设置为先前设置的targetTextField处的文本字段
- (void)fillContactName:(ABRecordRef)person
{
NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);
NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty);
self.targetTextField.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
}
请注意,我使用了
NSString
class方法stringWithFormat:
使fillContactName代码更加简洁。在这种情况下,这是一种常用的方法。这里有一种方法可以做到这一点
首先,在创建“浏览联系人”按钮时(或在情节提要中,如果使用情节提要),为其标记
属性指定唯一的int值,以便在getContactName:
方法中区分它们。我建议第一个按钮设置tag=0,第二个按钮设置tag=1
然后向ViewController类添加一个新属性,该属性将在单击按钮后存储指向目标文本字段的指针。(请确保在.m文件中@synthesis)
在getContactName:
中,检查发送者对象并使用其标记
值适当设置指针。(请注意,发件人将是用户单击的UIButton对象)
然后在fillContactName:
中,将文本值设置为先前设置的targetTextField处的文本字段
- (void)fillContactName:(ABRecordRef)person
{
NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);
NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty);
self.targetTextField.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
}
请注意,我使用了
NSString
class方法stringWithFormat:
使fillContactName代码更加简洁。在这种情况下,这是一种常用的方法。非常有效。谢谢,很有魅力。谢谢。在旁注中,您确定不想使用“ABRecordCopyCompositeName()”函数而不是手动拼凑名字和姓氏吗?在旁注中,您确定不想使用“ABRecordCopyCompositeName()”函数而不是手动拼凑名字和姓氏吗?
- (void)fillContactName:(ABRecordRef)person
{
NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);
NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty);
self.targetTextField.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
}