![](/img/trans.png)
[英]How to add a leftBarButtonItem to ABPeoplePickerNavigationController?
[英]how to get around lack of 'add' button in ABPeoplePickerNavigationController?
我的應用程序需要將自定義類的實例與iPhone的AddressBook中的聯系人記錄相關聯。 當我呈現ABPeoplePickerNavigationController並允許用戶選擇現有聯系人時,一切都很好。 問題是,有沒有明顯的方式,讓用戶可以輕松地添加一個聯系人記錄如果他們要找的人並不在他們的通訊錄已經存在。
人們如何以一種簡單直觀的方式從ABPeoplePickerNavigationController到ABNewPersonViewController ?
您可以創建一個UIBarButton並將其添加到ABPeoplePickerNavigationController的UINavigationBar中。
peoplePicker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson:)];
-(IBAction)addPerson:(id)sender{
ABNewPersonViewController *view = [[ABNewPersonViewController alloc] init];
view.newPersonViewDelegate = self;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:view];
[self.picker presentModalViewController:nc animated:YES];
}
我遇到的問題是ABPeoplePickerNavigationController有一個取消按鈕放在rightBarButtonItem插槽中,我不得不更新導航欄上的
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
我已經在我的博客上記錄了整個過程,其中包含一個可以讓您創建類似於iPhone上的聯系人樣式應用程序的工作示例 。 希望這可以幫助。
我發現Scott Sherwood的方法以及他在他的網站上發布的演示非常有幫助。 作為他博客上提到的評論者之一,編輯模式下的取消按鈕出現問題。
我剛剛提出修復Scott的演示,以及針對Person View Controller的不同方法: http : //finalize.com/2013/05/12/using-and-customizing-the-address-book-ui/
我對Person View Controller的建議是在協議方法peoplePickerNavigationController中手動啟動它:shouldContinueAfterSelectingPerson:用於ABPeoplePickerNavigationControllerDelegate。
// Displays the information of a selected person
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
ABPersonViewController *view = [[ABPersonViewController alloc] init];
view.personViewDelegate = self;
view.displayedPerson = person; // Assume person is already defined.
view.allowsEditing = YES;
view.allowsActions = YES;
[peoplePicker pushViewController:view animated:YES];
return NO;
}
這里唯一的問題是名稱的人員選取器表視圖在編輯后不會自動刷新。 這可以通過使用通訊簿回調來修復。 我將展示如何在我發布的GitHub項目中完成此操作:
似乎無法直接從ABPeoplePickerNavigationController添加新聯系人。 因此,當用戶單擊添加按鈕時,我將呈現帶有兩個按鈕的UIActionSheet:
- (void) addContact{
contactMenu = [[UIActionSheet alloc]
initWithTitle: nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle: nil
otherButtonTitles:@"Select a contact", @"Add a new contact", NULL];
[contactMenu showInView:self.view];
}
這是關聯的委托方法:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0){
// select an existing contact
ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
[self presentModalViewController:peoplePicker animated:YES];
}
if(buttonIndex == 1){
// add a new contact
ABNewPersonViewController *newPersonViewController = [[ABNewPersonViewController alloc] init];
newPersonViewController.newPersonViewDelegate = self;
UINavigationController *personNavController = [[UINavigationController alloc] initWithRootViewController:newPersonViewController];
[self presentModalViewController:personNavController animated:YES];
[personNavController release];
[newPersonViewController release];
}
if(buttonIndex == 2){
// cancel the operation
[actionSheet dismissWithClickedButtonIndex:2 animated:YES];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.