我意识到这个问题已被提前询问,但这些线程中没有提到任何问题。很多人似乎都有这个问题,因为委托是在viewDidLoad中设置的,但是你可以从下面看到,这不是我设置的地方。 “取消”操作是否有效,因此委托可以工作但不能实际选择。
声明的协议:
@interface MyTrackDetailsTVC () <UITextFieldDelegate,UIImagePickerControllerDelegate,ABPeoplePickerNavigationControllerDelegate>
定义了实例变量:
@property (strong, nonatomic) ABPeoplePickerNavigationController *picker;
通过按钮点击启动流程:
- (IBAction)importContactTapped:(UIButton *)sender
{
self.picker = [[ABPeoplePickerNavigationController alloc] init];
self.picker.peoplePickerDelegate = self;
[self presentViewController:self.picker animated:YES completion:nil];
}
取消方法效果很好:
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[[peoplePicker presentingViewController]dismissViewControllerAnimated:YES completion:nil];
}
从未调用选择方法(使用断点验证)。
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
NSString *fName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSData *imgData = (__bridge_transfer NSData *)ABPersonCopyImageData(person);
self.nameField.text = [NSString stringWithFormat:@"(%@ %@",fName,lName];
self.phoneField.text = lName;
self.imageView.image = [UIImage imageWithData:imgData];
[[peoplePicker presentingViewController]dismissViewControllerAnimated:YES completion:nil];
}
答案 0 :(得分:1)
选择联系人上的特定属性时,将调用peoplePickerNavigationController:didSelectPerson:property:identifier:
委托方法。如果您想知道何时选择联系人,请使用peoplePickerNavigationController:didSelectPerson:
委托方法。
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
NSString *fName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSData *imgData = (__bridge_transfer NSData *)ABPersonCopyImageData(person);
self.nameField.text = [NSString stringWithFormat:@"(%@ %@",fName,lName];
self.phoneField.text = lName;
self.imageView.image = [UIImage imageWithData:imgData];
[[peoplePicker presentingViewController]dismissViewControllerAnimated:YES completion:nil];
}