0

我是 Xcode 的新手,我试图让我的应用程序访问通讯簿,选择一个人,然后为这个人创建 NSString 值(名字、姓氏、组织、地址、电子邮件和电话号码)我可以拉名字和姓氏,组织,输入的第一封电子邮件(最好显示所有电子邮件地址,并让用户选择),以及输入的第一个电话号码(再次,如果能够选择),但此人的地址始终为空白。我非常感谢您能提供的任何帮助。此外,我不断收到本地声明隐藏实例变量警告 - 我不知道如何解决这些问题。

#import "TACustomer.h"

@interface TACustomer ()

@end

@implementation TACustomer

@synthesize custfirstName;
@synthesize custlastName;
@synthesize custOrganization;
@synthesize custEmail;
@synthesize custAddress;
@synthesize custphoneNumber;



- (IBAction)showPicker:(id)sender
{
    // Creating the Address Book Picker
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    // Place the delegate of the picker to the control.
    picker.peoplePickerDelegate = self;

    // Showing the picker.
    [self presentModalViewController:picker animated:YES];

}



- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    //assigning control back to the main controller.
    [self dismissModalViewControllerAnimated:YES];

}



- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{

    [self displayPerson:person];

    [self dismissModalViewControllerAnimated:YES];

    return NO;

}



- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
                            property:(ABPropertyID)property
                          identifier:(ABMultiValueIdentifier)identifier
{


    // Only inspect the value if it's an address.
    if (property == kABPersonAddressProperty)
    {

        //Set up an ABMultiValue to hold the address values; copy from a book record.
        ABMutableMultiValueRef multicustValue = ABRecordCopyValue(person, property);


        // Set up an NSArray and copy values into it.
        NSArray *thecustArray = (__bridge id)ABMultiValueCopyArrayOfAllValues(multicustValue);


        // Figure out which values we want and store the index.
        const NSUInteger customerIndex = ABMultiValueGetIndexForIdentifier (multicustValue, identifier);

        // Set up an NSDictionary to hold the contents of the array.
        NSDictionary *custDict = [thecustArray objectAtIndex:customerIndex];

        // Set up NSStrings to hold the keys and values. First, how many are there?
        const NSUInteger theCount = [custDict count];
        NSString * __unsafe_unretained keys[theCount];
        NSString *__unsafe_unretained values[theCount];

        // Get the keys and values from the CFDictionary.
        [custDict getObjects:values andKeys:keys];



        // Set the address label's text.
        NSString *customeraddress;
        customeraddress = [NSString stringWithFormat:@"%@, %@, %@, %@, %@",
                   [custDict objectForKey:(NSString *)kABPersonAddressStreetKey],
                   [custDict objectForKey:(NSString *)kABPersonAddressCityKey],
                   [custDict objectForKey:(NSString *)kABPersonAddressStateKey],
                   [custDict objectForKey:(NSString *)kABPersonAddressZIPKey],
                   [custDict objectForKey:(NSString *)kABPersonAddressCountryKey]];

        self.custAddress.text = customeraddress;


    }
        return NO;
}



- (void)displayPerson:(ABRecordRef)person
{

    // Get Customer First Name
    NSString* custfirstname = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);

    self.custfirstName.text = custfirstname;


    // Get Customer Last Name
    NSString* custlastname = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty);

    self.custlastName.text = custlastname;


    // Get Customer Organization
    NSString* custorganization = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonOrganizationProperty);

    self.custOrganization.text = custorganization;



    //Get Customer Email Address
    NSString* custemail = nil;
    ABMultiValueRef custemailAddresses = ABRecordCopyValue (person,kABPersonEmailProperty);

    if (ABMultiValueGetCount(custemailAddresses) > 0)
    {
        custemail = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(custemailAddresses, 0);

    } else
    {
        custemail = @"[None]";

    }
    self.custEmail.text = custemail;
    CFRelease(custemailAddresses);


    // Get Customer Phone Number
    NSString* custphone = nil;
    ABMultiValueRef phoneNumbers = ABRecordCopyValue (person,kABPersonPhoneProperty);

    if (ABMultiValueGetCount(phoneNumbers) > 0)
    {
        custphone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

    } else
    {
        custphone = @"[None]";

    }
    self.custphoneNumber.text = custphone;
    CFRelease(phoneNumbers);


bundle:nil;

//[self.navigationController pushViewController:tempExamInfoView animated:YES];
}

@end
4

1 回答 1

0

对于地址数据为空的问题,我没有答案。

要摆脱“本地声明隐藏实例变量”警告,您需要使用不与您正在合成的属性名称冲突的不同变量名称。

例如,在displayPerson:你有一个局部变量custfirstname。因为它使用与属性相同的名称,所以局部变量隐藏了正在合成的同名实例变量。

如果您想保留局部变量名称,我相信也可以告诉它为@synthesize它生成的实例变量使用不同的名称。我不熟悉语法,所以如果你想这样做,你必须自己查一下。

于 2013-01-23T00:42:18.810 回答