How to Use a ABPersonViewController without connec

2020-07-27 16:36发布

问题:

Setup: I'm looking for insight into using a ABPersonViewContoller without getting data from the AddressBook. I have a JSON web service that is feeding a tableview and I have my person objects stored in a mutableArray as dictionaries.

my Question: I've been searching and been unable to find any reference to using the personViewController with your own objects. Can I init a new personView assign it my person object and push the controller? or would I be better off rolling my own view to duplicate the apple one?

Update: As Eric suggested using a ABRecordRef as my object and it does infact work. here's a code example.

    // get index and set person object
    int personIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    ABRecordRef person = ABPersonCreate();

    ABRecordSetValue(person, kABPersonFirstNameProperty, [[personList objectAtIndex: personIndex] objectForKey: @"firstName"], nil);
    ABRecordSetValue(person, kABPersonLastNameProperty, [[personList objectAtIndex:personIndex] objectForKey:@"lastName"], nil);

    // initialize a personviewController
    ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];

    personViewController.personViewDelegate = self; 
    personViewController.displayedPerson = person;
    personViewController.allowsEditing = NO;

    // push to view controller
    [[self navigationController] pushViewController:personViewController animated:YES];
    [personViewController release];

hope this helps others.

回答1:

The ABPersonViewController is tied to the iOS device's AddressBook (with the whole Address Book UI Framework), so I believe it would be easier to roll your own UIViewController to get what you want. Also, the docs note that "Person view controllers must be used with a navigation controller in order to function properly." So you are (possibly) limited there.

That said (and I haven't tried it), but it does appear that the displayedPerson attribute is readwrite, so I suppose if your data was stored in your array as an ABRecordRef it might work.