【问题标题】:Updating UITextField with contact info selected from iPhone Contacts使用从 iPhone 联系人中选择的联系人信息更新 UITextField
【发布时间】:2023-03-10 17:05:01
【问题描述】:

在调用 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;
}

【问题讨论】:

    标签: objective-c ios uitextfield addressbook


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      -(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];
      }
      

      【讨论】:

        猜你喜欢
        • 2018-04-10
        • 1970-01-01
        • 2021-11-21
        • 2017-05-10
        • 2013-06-03
        • 2012-02-21
        • 1970-01-01
        • 2019-06-03
        • 1970-01-01
        相关资源
        最近更新 更多