0

我的 Objective C 学习进展顺利,但在我的应用程序中遇到了问题。

下面是我到目前为止的代码,只是一个简单的单按钮应用程序,计划是单击按钮将打开联系人,让您选择一个联系人,然后从可用的联系人中选择一个特定号码,该号码保存为最终成为一个领域。我已经做到了这一点,但我点击联系人列表中的号码并没有任何反应。

.h 文件:

#import <UIKit/UIKit.h>
#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>

@interface ViewController : UIViewController <UINavigationControllerDelegate,   ABPeoplePickerNavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *myLabel;
- (IBAction)myButton:(UIButton *)sender;

@end

.m 文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
 [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)myButton:(UIButton *)sender {
ABPeoplePickerNavigationController *peoplePicker =
[[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
[self presentViewController:peoplePicker animated:YES completion:nil];
}

- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)picker
   shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
return YES;
}

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

- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)pickershouldContinueAfterSelectingPerson :
(ABRecordRef)person  {

NSString* name =
(__bridge_transfer NSString *)ABRecordCopyCompositeName(person);

ABMutableMultiValueRef phones =
ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray *numbers =
(__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(phones);

ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
NSString *addresses =
(__bridge_transfer NSString *)ABMultiValueCopyArrayOfAllValues(emails);

NSString *note =
(__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonNoteProperty);

NSLog( @"name = %@, numbers = %@, email = %@, note = %@",
      name, numbers, addresses, note );

//    WANT TO MAKE FOR EXAMPLE ( myTextField.text = CHOSEN NUMBER OF CONTACT

[self dismissViewControllerAnimated:YES completion:nil];
return YES;
}
@end
4

1 回答 1

0

ABPersonViewControllerDelegate首先,在 .h 文件中声明您的符合性。

其次,在您的类中实现以下ABPersonViewControllerDelegate回调方法来枚举所选联系人的电话号码:

#pragma mark - ABPersonViewControllerDelegate protocol conformance

- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue
{
    BOOL shouldPerformDefaultAction = YES;

    // Perform special action if phone number was selected
    if (property == kABPersonPhoneProperty)
    {
        CFTypeRef phoneProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
        NSArray *phones = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);
        CFRelease(phoneProperty);

        for (NSString *phone in phones)
        {
            NSLog(@"phone = %@", phone);
        }

        shouldPerformDefaultAction = NO;

        [self.navigationController popViewControllerAnimated:YES];
    }

    // Otherwise, allow the default action to occur.
    return shouldPerformDefaultAction;
}
于 2013-11-13T00:13:56.390 回答