3

我正在尝试遵循 Apple 的地址簿编程指南,并对 peoplePickerController 有疑问。Apple 教程中的示例照片在右上角有“加号”按钮,这似乎允许用户通过 peoplePickerController 添加新联系人。

我通过调用 ABPeopleNavigationControllerPicker


- (void)showPicker
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;

    [self presentViewController:picker animated:YES completion:nil];
}

但是屏幕右上角有一个取消按钮,而不是加号按钮。是否有任何选项可以更改按钮或允许用户通过此视图控制器添加新联系人?

在谷歌搜索了一段时间后,我找到了以下内容。所以我尝试以编程方式更改栏按钮......


- (void)addPerson
{
    ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init];
    newPersonVC.newPersonViewDelegate = self;
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:newPersonVC];
    [self presentViewController:nc animated:YES completion:nil];
}
- (void)showPicker
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

但是,不知何故,栏按钮仍然固定为“取消”而不是“添加”......

4

3 回答 3

2

在 iOS 7 上,我需要稍后设置这些按钮...

 - (void)showPicker
 {
     ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
      picker.peoplePickerDelegate = self;
     [self presentViewController:picker animated:YES completion:nil];
   [picker.topViewController setEdit:YES];
   [self performSelector:@selector(setThemBuddons:) withObject:picker afterDelay:0.1];
}

-(void)setThemBuddons:(ABPeoplePickerNavigationController*)picker;
{
     picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];

}
于 2013-11-04T22:39:08.163 回答
0

没关系,我现在开始工作了。看来我需要在覆盖其按钮之前呈现 ViewController ......


- (void)showPicker
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
    [picker.topViewController setEdit:YES];
    picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];
}

于 2013-01-10T14:49:35.323 回答
-1

您只需要对 addPerson 方法进行一点修改。

用于显示联系人:

-(无效)显示选择器

{

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController       alloc] init];
  picker.peoplePickerDelegate = self;
  [self presentViewController:picker animated:YES completion:nil];
  [picker.topViewController setEdit:YES];
  [self performSelector:@selector(addContact:) withObject:picker afterDelay:0.1];

}

-(void)addContact:(ABPeoplePickerNavigationController*)picker;

{

picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];

}

通过此代码,您将在右上角获得一个加号以添加联系人。

我们为此使用@selector。

-(void)addPerson

{

ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController 
alloc] init];
newPersonVC.newPersonViewDelegate = self;
UINavigationController *nc = [[UINavigationController alloc]     initWithRootViewController:newPersonVC];

//这是完成工作的代码。通过使用self.presentedViewController,我们可以//获取添加联系人屏幕,否则我们将收到类似空视图层次结构的错误。

    [self.presentedViewController presentViewController:nc animated:YES completion:nil];

}

于 2014-06-16T16:38:22.850 回答