How to pass data between two controllers in which one controller is abadressbook fetched in uitableviw

79 views Asked by At

I created an ABAddressBook on a UITableView. Now, I need to pass the firstname, last name, etc. to next viewcontroller.

I have to pass data using nsobject class ... in this project i have made Person class which has string properties of name,lastname,number,email.

The code for address book is:

ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
allPeople = (__bridge NSMutableArray *)(ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonFirstNameProperty));
NSInteger numberOfPeople = [allPeople count];
for (NSUInteger i = 0; i < numberOfPeople; i++) {
    personContact = [[Person alloc]init];

    person = (__bridge ABRecordRef)allPeople[i];

    NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
    if (firstName == nil ) {
        firstName = @"";
    }
    if (lastName == nil)
    {
        lastName = @"";
    }

    NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];

    personContact.firstName = firstName;
    personContact.lastName = lastName;
    personContact.fullName = fullName;

//For adding multiple contacts:



ABMultiValueRef phoneNumbers = ABRecordCopyValue((person), kABPersonPhoneProperty);

    CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
    for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
        NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));

        if ([phoneNumber isEqualToString:@""])
        {
            phoneNumber = @"Not Available";
        }

        NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"#();$&-+"];
        phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet: trim] componentsJoinedByString: @""];
        phoneNumber= [phoneNumber stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@"" withString:@""];

        personContact.phoneNumber = phoneNumber;

//Emails

        ABMutableMultiValueRef eMail  = ABRecordCopyValue(person, kABPersonEmailProperty);
        if(ABMultiValueGetCount(eMail) > 0)
        {
            email =CFBridgingRelease(ABMultiValueCopyValueAtIndex(eMail, 0));
        }
        else
        {
            email =  @"";
        }
        personContact.email = email;
    }

//Photos

    CFDataRef imgData = ABPersonCopyImageData(person);
    NSData *imageData = (__bridge NSData *)(imgData);

    phoneImage = [UIImage imageWithData:imageData];
    if (phoneImage == nil) {
        phoneImage = [UIImage imageNamed:@"userEdit"];
    }
    CGSize destinationSize = CGSizeMake(70, 70);
    UIGraphicsBeginImageContext(destinationSize);
    [phoneImage drawInRect:CGRectMake(0, 0, destinationSize.width, destinationSize.height)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    if (imgData != NULL)
    {
        CFRelease(imgData);
    }
    personContact.photos = (NSString *)newImage;

    [contactsData addObject:personContact];
    [contactstableView reloadData];

}
}


And the code for cellforrowatIndex path is as follows:- 
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"CellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell==nil)
{

   cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
   cell.backgroundColor=[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:239.0/255.0 alpha:1.0];
 }


if (tableView == self.searchDisplayController.searchResultsTableView) {
    personContact = [searchResults objectAtIndex:indexPath.row];

}
else {
    personContact = [contactsData objectAtIndex:indexPath.row];

}
if (contactsData.count > 0)
{
    cell.textLabel.text = personContact.fullName;

    cell.imageView.image = personContact.photos;
    cell.imageView.layer.cornerRadius = 35;
    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

  }

On didselect row, I want to pass all the contact details to next controller. Please help?

3

There are 3 answers

0
ios On

I am doing this in cellforrowAtIndexPath

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
     {
    AddGuestVC *add = [[AddGuestVC alloc]initWithNibName:@"AddGuestVC" bundle:nil];

    add.personContact = [contactsData objectAtIndex:indexpath.row];

    add.delegate = self;
   add.personContact = personContact;
    [self.navigationController pushViewController:add animated:YES];
}
}

and for populating textfield i use :

   firstname.text = self.personContact.firstName; 
9
Ankit Sachan On

If you have to pass it from ABAddressBook to ViewController use delegate methods

If you have to pass it to ABAddressBook you can Pass ABPerson or Associative properties

objc_setAssociatedObject(actionSheet, @"selectedUserJidStr", selectedUserJidStr, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
0
Sujay On

I guess you will display view on cell select, which you can do using following delegate method.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"segueName" sender:indexPath];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"segueName"])
    {
        NSIndexPath *indexPath = sender;
        ViewControllerName *objectOfViewControllerName = segue.destinationViewController;
        objectOfViewControllerName.personContact = [contactsData objectAtIndex:indexPath.row];
    }
}