我正在尝试使用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;
}
}
}
答案 0 :(得分: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