简体   繁体   English

使用通讯录在iOS中的联系人中添加编辑功能

[英]Add edit functionality in contacts in iOS using Addressbook

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. 我正在尝试使用AddressBook在iOS的Xcode 4.2中创建一个简单的创建,显示和编辑联系人,到目前为止,我已经完成了创建和显示功能。 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. 下面是到目前为止我完成的ViewController.m代码。 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. 如果我在tableview didselectRowAtIndexPath属性中进行这样的编码,则可以得到所需的结果,但是我想在我的displayContacts方法而不是tableview中实现并集成此代码。 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??? 一旦进入显示联系人,还有其他方法可以获取当前所选名称的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;
            }
    }

}
-(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 在此功能的帮助下获得了解决方案,但现在只能添加删除功能:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用iOS Addressbook api搜索交换联系人 - Using iOS Addressbook api to search exchange contacts 如何使用AddressBook框架但不使用新的Contacts Framework从ABAddressBook iOS获取统一联系人? 可能吗? - How to fetch unified contacts from ABAddressBook iOS using AddressBook framework but by not using new Contacts Framework ? Is it even possible? 如何在iOS中通过AddressBook与电子邮件联系? - How to get contacts with Email from AddressBook in iOS? 根据AddressBook iOS中最近使用/最常用/紧急的排序联系人 - Sorting contacts based on recently used/most used/urgency in AddressBook iOS iOS通过UnitTest访问AddressBook Contacts; 如何设置权限? - iOS accessing AddressBook Contacts via UnitTest; how to set permissions? 如何从数组中删除Facebook联系人? iOS地址簿 - How can I remove facebook contacts from array? Ios AddressBook 如何知道ios通讯录是否可以访问Facebook联系人 - How to know if the ios addressbook can access Facebook contacts 如何从AddressBook中获取联系人并将其存储在iOS 7中的数组中? - How to fetch contacts from AddressBook and store it in array in iOS 7? whatsapp如何在iOS中更快地从地址簿获得更新的联系人? - How whatsapp get updated contacts from addressbook faster in iOS? 如何从AddressBook中获取联系人并将其存储在iOS 6中的数组中? - How to fetch contacts from AddressBook and store it in array in iOS 6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM