【问题标题】:How do I tell the address book picker which fields to fill out?我如何告诉通讯录选择器填写哪些字段?
【发布时间】:2012-02-23 17:01:37
【问题描述】:

我对 iOS 开发非常陌生。因此,请尽可能准确和基本地回答您的问题。

我有一个包含两个(目前 - 可能还有更多)字段的表单,每个字段旁边都有一个按钮,允许用户从 iPad 通讯录中选择一个联系人,并在相关字段中填写第一个和地址簿中的姓氏。

The example code I have 让我可以填写联系人姓名。但是,我希望能够单击被推荐人旁边的“浏览联系人”按钮,并让它使用相同的功能来填写被推荐人的姓名。我看到 getContactName 函数有 sender 参数。所以,我可以很容易地分辨出这两个按钮中的哪一个(或以后的其他按钮)被点击了。

但是,当我从通讯录中选择时,我如何知道在 peoplePickerNavigationController 或 fillContactName 函数中单击了哪个按钮?

- (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;

}

【问题讨论】:

  • 附带说明,您确定不想使用“ABRecordCopyCompositeName()”函数而不是手动将名字和姓氏放在一起吗?

标签: ios xcode xcode4 ios5


【解决方案1】:

这是您可以做到的一种方法。

首先,在创建“浏览联系人”按钮时为其 tag 属性分配唯一的 int 值(或在情节提要中,如果您使用情节提要),以便您可以在 getContactName: 方法中区分它们.我建议为第一个按钮设置 tag=0,为第二个按钮设置 tag=1。

然后向 ViewController 类添加一个新属性,该属性将在单击按钮后存储指向目标文本字段的指针。 (一定要在你的 .m 文件中@synthesize)

@property (nonatomic, strong) UITextField *targetTextField;

getContactName: 中,检查发送者对象并使用其tag 值来适当地设置指针。 (请注意,发送者将是用户单击的 UIButton 对象)。

- (IBAction)getContactName:(id)sender {

    ...

    switch(sender.tag) {
        case 0:
            self.targetTextField = self.contactName;
            break;
        case 1:
            self.targetTextField = self.referredBy;
            break;
        default:
            self.targetTextField = nil;
    }           

    [self presentModalViewController:picker animated:YES];
}

然后在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 类方法stringWithFormat: 使您的fillContactName 代码更简洁。这是这种情况下常用的方法。

【讨论】:

  • 像魅力一样工作。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
  • 2010-11-01
  • 2013-12-12
  • 2021-08-05
  • 1970-01-01
相关资源
最近更新 更多