使用从iPhone联系人中选择的联系信息更新UITextField

时间:2011-11-25 19:46:53

标签: objective-c ios uitextfield addressbook

调用PeoplePicker并从“联系人”中选择一个条目后,我似乎无法在我的视图中显示所选的联系人信息。按照Apple的iOS地址簿编程指南,我以模态方式显示人物选择器并进行选择。 Apple的代码示例将信息显示在UILabels中,因为我还允许用户手动将信息输入到同一视图控制器的文本字段中,需要在UITextFields中显示信息。我已经通过打印到控制台证明所选数据在目标UITextField(名为“contactName”)中结束。问题是,所选数据不会显示在视图中。当用户手动输入时,没有问题。我无法弄清楚如何将数据从通讯簿传输到视图控制器中的UITextFields。

我已经尝试了几天,但无法解决这个问题。我可能忽略了一些非常基本的东西。我是新手,所以任何帮助都会受到赞赏。

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

   // NSString* name = (NSString *)ABRecordCopyValue(person,kABPersonFirstNameProperty);
    NSString *name = [NSString stringWithFormat:@"%@ %@",ABRecordCopyValue(person,kABPersonFirstNameProperty), ABRecordCopyValue(person,kABPersonLastNameProperty)];

    self.contactName.text = name;   // 11/17/11

    NSLog(@"%@", name);
    // [name release];  Commenting out this code prevents the app from freezing when a name is selected from the picker.  11/25/11
    NSLog(@"%@",self.contactName.text);                // 11/17/11

    // 11/16/11 add additional contact info here

    [self dismissModalViewControllerAnimated:YES];
    return NO;
}

2 个答案:

答案 0 :(得分:0)

-(IBAction)addToAddressbook:(id)sender{
    NSString *fname=@"firstname";
    NSString *lname=@"lastname";
    NSArray *arrayAdd=[[NSArray alloc]initWithObjects:@"street Name",@"city Name",@"country code",@"zip",nil];
    UIImage *image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ambani.png" ofType:nil]];
    [self addContact:fname:lname:arrayAdd:image];
}

-(void) addContact:(NSString *)firstname:(NSString *)lastname:(NSArray *)arrayAddress:(UIImage *)currentImage
{
    ABAddressBookRef addressBook=ABAddressBookCreate();
    ABRecordRef person=ABPersonCreate();
    //set Image
    NSData * dataRef = UIImagePNGRepresentation(currentImage);
    ABPersonSetImageData(person, (CFDataRef)dataRef, nil);
    //set FirstName and LastName
    ABRecordSetValue(person, kABPersonFirstNameProperty,firstname, nil);
    ABRecordSetValue(person, kABPersonLastNameProperty,lastname, nil);
    //Add Address
    ABMutableMultiValueRef address=ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDictionary=[[NSMutableDictionary alloc]init];
    [addressDictionary setObject:[arrayAddress objectAtIndex:0] forKey:(NSString *)kABPersonAddressStreetKey];
    [addressDictionary setObject:[arrayAddress objectAtIndex:1]forKey:(NSString *)kABPersonAddressCityKey];
    [addressDictionary setObject:[arrayAddress objectAtIndex:2] forKey:(NSString *)kABPersonAddressCountryCodeKey];
    [addressDictionary setObject:[arrayAddress objectAtIndex:3] forKey:(NSString *)kABPersonAddressCountryKey];
    ABMultiValueAddValueAndLabel(address, addressDictionary, kABHomeLabel, nil);
    ABRecordSetValue(person, kABPersonAddressProperty, address, nil);
    ABAddressBookAddRecord(addressBook,person, nil);
    ABAddressBookSave(addressBook,nil); 
    objABPersonViewController=[[ABUnknownPersonViewController alloc]init];
    objABPersonViewController.displayedPerson=person;
    [self.navigationController pushViewController:objABPersonViewController animated:YES];
    CFRelease(person);
}

-(void) validateAndDisplayContacts
{
    if([firstName length]==0)
    {
        firstName=@" ";
    }
    lblName.text=[NSString stringWithFormat:@"%@ : %@",@"First Name",name];
    [firstName release];
    if([mobile length]==0)
    {
        mobile=@" ";
    }
    lblPhone.text=[NSString stringWithFormat:@"%@ : %@",@"Mobile",mobile];
    [mobile release];

    if([email length]==0)
    {
    email=@" ";
    }
    lblEmail.text=[NSString stringWithFormat:@"%@ : %@",@"Email",email];
    [email release];
}


- (void)unknownPersonViewController:(ABUnknownPersonViewController *) unknownCardViewController didResolveToPerson:(ABRecordRef)person
{
    [objABPersonViewController dismissModalViewControllerAnimated:YES];
}

答案 1 :(得分:0)

从iOS 6开始,不推荐使用[self dismissModalViewControllerAnimated]。如果你试试这个,你的代码就可以了。

[self dismissViewControllerAnimated:YES completion:^(void) {
NSString *name = [NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)];
contactName.text=name;
}];