我的 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