0

I'm trying to create a simple create, display and edit contacts in Xcode 4.2 for iOS using AddressBook, so far I have done the create and display function. Now I need to implement edit function along with display. Now when I click on display it displays all the contacts and when I click on any name it shows info with a back button and a cancel button. I need to change the cancel button to edit and call the edit functionality. Below is the code of ViewController.m which I have done so far. Appreciate if u could give me a solution.

#import "ViewController.h"
#import <AddressBook/AddressBook.h>
enum MainMenuChoice
{
    menuDisplayContacts,
    menuCreateContacts

};

@implementation ViewController

@synthesize menuArray;

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];

    NSLog(@"%@",plistPath);
    self.menuArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
}

- (void)viewDidUnload { 
    self.menuArray = nil;   
}

#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [menuArray count];
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (aCell == nil) {
         // Make the rows look like buttons

          aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
          aCell.textLabel.textAlignment = UITextAlignmentCenter;

    }

    aCell.textLabel.text = [[menuArray objectAtIndex:indexPath.section] valueForKey:@"title"];

    return aCell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case menuDisplayContacts: 
            [self showContacts];
            break;

        case menuCreateContacts:
            [self createContacts];
            break;

        default:
            [self showContacts];
            break;
    }
}


//Show list of all people in Contacts
- (void)showContacts {
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc]init];
    picker.peoplePickerDelegate = self;

   NSArray *displayItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],[NSNumber numberWithInt:kABPersonEmailProperty], [NSNumber numberWithInt:kABPersonBirthdayProperty],nil];

    picker.displayedProperties = displayItems;

    [self presentModalViewController:picker animated:YES];

}

// Dismisses the people picker and shows the application when users tap Cancel. 
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
    [self dismissModalViewControllerAnimated:YES];
}

//For creating new contact when user tap create contacts
-(void)createContacts {

    ABNewPersonViewController *picker = [[ABNewPersonViewController alloc]init];
    picker.newPersonViewDelegate=self;

    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
    [self presentModalViewController:navigation animated:YES];

}

// Dismisses the new-person view controller when user tap cancel. 
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person {
    [self dismissModalViewControllerAnimated:YES];
}

@end

I can get the desired result if I code like this in the tableview didselectRowAtIndexPath property, but I want to implement and integrate this code inside my displayContacts method instead of tableview here. Because this method is for initial screen for display and create contacts. Is there any other way that I could get the indexPath of the current selected names once I am inside the display contacts???

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    ABPersonViewController *DVC=[[ABPersonViewController alloc]init];

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL,NULL);
    NSMutableArray *allPeople = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
    int nPeople = ABAddressBookGetPersonCount(addressBook);
    for(int i=0;i<nPeople;i++) {

        ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);
        NSString *name = [self.contactAdd objectAtIndex:indexPath.row],*name2;
        if(ABRecordCopyValue(person, kABPersonFirstNameProperty) != NULL) {
            name2 = [[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)]
                     stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            name2=[name2 stringByAppendingString:@" "];
            name2=[name2 stringByAppendingString:[[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonLastNameProperty)]
                                                  stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
        }
        if([name isEqualToString:name2])
            {
                DVC.displayedPerson=person;
                DVC.allowsEditing=YES;
                [self.navigationController pushViewController:DVC animated:YES];
                break;
            }
    }

}
icodebuster
  • 8,890
  • 7
  • 62
  • 65
Francis F
  • 3,157
  • 3
  • 41
  • 79

1 Answers1

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

            [peoplePicker dismissModalViewControllerAnimated:NO];

            ABPersonViewController *picker = [[ABPersonViewController alloc] init];
            picker.personViewDelegate = self;
            picker.displayedPerson = person;
            // Allow users to edit the person’s information
            picker.allowsEditing = YES;
            [self.navigationController pushViewController:picker animated:YES];



    return YES;
}

Got the solution with help of this function, but now stuck in adding delete function :P

Francis F
  • 3,157
  • 3
  • 41
  • 79