0

我正在尝试使用 AddressBook 在 Xcode 4.2 for iOS 中创建一个简单的创建、显示和编辑联系人,到目前为止我已经完成了创建和显示功能。现在我需要实现编辑功能和显示。现在,当我单击显示时,它会显示所有联系人,当我单击任何名称时,它会显示带有后退按钮和取消按钮的信息。我需要更改取消按钮以进行编辑并调用编辑功能。下面是我到目前为止所做的 ViewController.m 的代码。感谢你能给我一个解决方案。

#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

如果我在 tableview didselectRowAtIndexPath 属性中这样编码,我可以获得所需的结果,但我想在我的 displayContacts 方法而不是 tableview 中实现和集成此代码。因为此方法是用于初始屏幕的显示和创建联系人。一旦我在显示联系人中,有没有其他方法可以获得当前所选名称的 indexPath ???

- (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;
            }
    }

}
4

1 回答 1

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;
}

借助此功能获得了解决方案,但现在卡在添加删除功能:P

于 2013-08-01T04:03:29.050 回答