2009-06-24 21 views
3

我的應用程序需要將自定義類的實例與iPhone的AddressBook中的聯繫人記錄關聯。當我提供ABPeoplePickerNavigationController並允許用戶選擇現有的聯繫人時,一切都很好。問題是沒有明顯的方法允許用戶輕鬆地添加聯繫人記錄,如果他們正在尋找的記錄不存在已在其AddressBook中存在如何解決ABPeoplePickerNavigationController中缺少'添加'按鈕?

如何人的ABPeoplePickerNavigationControllerABNewPersonViewController獲得的方式,很容易&直觀的用戶?

回答

1

它似乎無法直接從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]; 
    } 


} 
+1

你錯過了「[peoplePicker發佈];」在你的(buttonIndex == 0) – Meltemi 2009-08-12 17:41:02

6

您可以創建一個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{ 

導航欄我已經記錄了整個過程我的blogworked example,應該允許你創建一個類似於iPhone上的聯繫人風格的應用程序。希望這可以幫助。

+0

這是否適用於iOS 8+? – hgwhittle 2015-07-29 22:19:13

3

我發現斯科特·舍伍德的方法和他在他的網站上發佈的演示非常有幫助。正如他的博客中提到的評論者之一,編輯模式下的取消按鈕存在問題。

我只是提出了一個修正斯科特的演示,與對人視圖控制器不同的方法一起: http://finalize.com/2013/05/12/using-and-customizing-the-address-book-ui/

我對人視圖控制器的建議是手動將它在協議方法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; 
} 

這裏唯一的問題是名稱的People Picker表視圖在編輯後不會自動刷新。這可以通過使用通訊簿回調來解決。我將展示如何這可以在GitHub的項目,我在發佈完成:

https://github.com/scottcarter/AddressBookPeoplePicker.git

相關問題