【问题标题】:how to get around lack of 'add' button in ABPeoplePickerNavigationController?如何解决 ABPeoplePickerNavigationController 中缺少“添加”按钮的问题?
【发布时间】:2009-06-24 22:56:20
【问题描述】:

我的应用需要将自定义类的实例与 iPhone 通讯录中的联系人记录相关联。当我展示 ABPeoplePickerNavigationController 并允许用户选择 现有 联系人时,一切都很好。问题是如果用户正在寻找的联系人记录已经不存在于他们的地址簿中,那么没有明显的方法可以让用户轻松添加联系人记录。

人们如何以对用户来说简单直观的方式从 ABPeoplePickerNavigationControllerABNewPersonViewController

【问题讨论】:

    标签: iphone cocoa-touch addressbook


    【解决方案1】:

    您可以像这样创建一个 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{
    

    我已经在我的blog 上记录了整个过程,worked example 应该允许您创建类似于 iPhone 上的联系人样式应用程序。希望这会有所帮助。

    【讨论】:

    • 我没有在 iOS 8 上测试过,所以很遗憾我无法确认。
    【解决方案2】:

    我发现 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 项目中展示了如何做到这一点:

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

    【讨论】:

      【解决方案3】:

      似乎无法直接从 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];
          }
      
      
      }
      

      【讨论】:

      • 您缺少“[peoplePicker release];”在您的 (buttonIndex == 0) 下
      猜你喜欢
      • 2021-06-20
      • 1970-01-01
      • 2010-10-29
      • 2011-10-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多