我正在使用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
答案 0 :(得分:0)
首先,在您的.h文件中声明您与ABPersonViewControllerDelegate
的一致性。
其次,在您的班级中实施以下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;
}