无法从iPhone应用程序编辑通讯簿记录

时间:2012-04-23 10:42:12

标签: iphone edit addressbook abpersonviewcontroller

我想在我的iphone应用中编辑地址簿中的记录。但我无法编辑任何记录。这是我的代码

 // In my First View Controller

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   DetailViewController *detailViewController=[[DetailViewController alloc] initWithRecordObject:record];

 [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}

//------------------------------------

#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "User.h"

@interface DetailViewController : UIViewController <UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate,ABPersonViewControllerDelegate> {

    ABRecordRef record;
    // Some other ivars
}
- (id)initWithRecordObject:(ABRecordRef)myrecord;

//------------------------------------

@implementation DetailViewController

- (id)initWithRecordObject:(ABRecordRef)myrecord
{
    self = [super initWithNibName:@"DetailViewController" bundle:nil];
    if (self) {
        record = myrecord;
    }
    return self;
}

#pragma mark - Edit Record Method

-(void)btnEditContactTapped:(id)sender {

    // Fetch the address book 
    ABAddressBookRef addressBook = ABAddressBookCreate();

    ABRecordID recID = ABRecordGetRecordID(record);

    ABRecordRef record1 = ABAddressBookGetPersonWithRecordID(addressBook,recID);

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

    // set delegate
    personViewController.personViewDelegate = self;

    // Allow editing info
    personViewController.allowsEditing = YES;

    // Display contact info of selected person
    personViewController.displayedPerson = record1;

    personViewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(returnFromPersonView)] ;

    APP_DELGATE.isContactEdited = YES;

    [self.navigationController pushViewController:personViewController animated:YES];

    [personViewController release];
}

-(void)returnFromPersonView {
    NSLog(@"In %s",__PRETTY_FUNCTION__);
    [self.navigationController popViewControllerAnimated:YES];

}

#pragma mark - ABPersonViewControllerDelegate Method

- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue {

    //[self dismissModalViewControllerAnimated:YES];
    return NO;
}

当我推送personViewController时,我看不到有关记录的任何内容。这是一个截图

enter image description here

非常感谢任何形式的帮助。谢谢

3 个答案:

答案 0 :(得分:0)

看一下Apple的Adress Book Programming Guide。对您有用的页面是Direct Interaction: Programmatically Accessing the Database页面。我建议你只是环顾这些页面,它们是非常自我解释的。

答案 1 :(得分:0)

将返回更改为YES可能有助于最后一种方法

- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue {

//[self dismissModalViewControllerAnimated:YES];
return NO; //try changing it to YES
}

答案 2 :(得分:0)

您没有将“正确的联系ID”传递为ABRecordRef;)