使用storyboard segue显示ABPeoplePickerNavigationController [英] Display ABPeoplePickerNavigationController using storyboard segue

查看:212
本文介绍了使用storyboard segue显示ABPeoplePickerNavigationController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个新的项目,我想显示一个人物选择器,因为我有一个 UIButton ,其转换为具有标识符 showContacts 的通用 UIViewController 。我设置这个ViewController的类为 ABPeoplePickerNavigationController



现在在我的根ViewController我有这个代码来初始化我的picker:

  #pragma mark  -  Segues 

- (void)prepareForSegue:(UIStoryboardSegue *)segue发件人:(id)发件人{

if([segue.identifier isEqualToString:@showContacts]){
ABPeoplePickerNavigationController * ppnc = segue.destinationViewController;
ppnc.peoplePickerDelegate = self;
ppnc.addressBook = ABAddressBookCreate();
ppnc.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
}
}

虽然我已将测试联系人添加到我的模拟器地址簿结果如下所示:





使用以下代码,这与我在 prepareForSegue:方法中做的非常相似,我设法显示一个选择器通过 IBAction

   - (IBAction)showPicker: sender {

ABPeoplePickerNavigationController * picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
NSArray * displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonBirthdayProperty],nil];
picker.displayedProperties = displayedItems;
//显示选择器
[self presentModalViewController:picker animated:YES];
}

结果:





我不清楚为什么人们选择器不显示。

解决方案

奇怪的是我建议的我可以使用视图控制器的指定初始化器吗?修复了我的问题。所以为 ABPeoplePickerNavigationController 创建一个代理ViewController修复了这个问题,但是没有解释为什么内建的选择器不能在storyboard中使用:



这是我的包装类的代码:

  #importPeoplePickerViewControllerWrapper.h 

@implementation PeoplePickerViewControllerWrapper

@synthesize ppvc = _ppvc; //这是我代理的对象(被代理人可以这么说)
@synthesize delegate = _delegate;

- (void)awakeFromNib
{
self.ppvc = [[ABPeoplePickerNavigationController alloc] init];
self.ppvc.peoplePickerDelegate = self;
self.ppvc.addressBook = ABAddressBookCreate();
self.ppvc.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
}

#pragma mark - 查看生命周期

- (void)loadView
{
[super loadView];
[self.ppvc loadView];
}

- (void)viewDidLoad
{
[super viewDidLoad];
[self.ppvc viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.ppvc viewWillAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.ppvc viewDidDisappear:animated];
}

- (UIView *)view {
return self.ppvc.view;
}

- (void)viewDidUnload
{
[super viewDidUnload];
[self.ppvc viewDidUnload];
}


I have a new project where I want to display a People Picker, when a button is touched.

So I have a UIButton that segues to a generic UIViewController with the identifier showContacts. I set the class of this ViewController to ABPeoplePickerNavigationController.

Now in my root ViewController I have this code to initialize my picker:

#pragma mark - Segues

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if([segue.identifier isEqualToString:@"showContacts"]){
        ABPeoplePickerNavigationController *ppnc = segue.destinationViewController;
        ppnc.peoplePickerDelegate = self;
        ppnc.addressBook = ABAddressBookCreate(); 
        ppnc.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
    }
}

Although I have added test contacts to my Simulator Address book the results looks like this:

With the following code, which is very similar to what I do in the prepareForSegue: method, I manage to show a picker via an IBAction:

- (IBAction)showPicker:(id)sender {

    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty], 
                               [NSNumber numberWithInt:kABPersonEmailProperty],
                               [NSNumber numberWithInt:kABPersonBirthdayProperty], nil];
    picker.displayedProperties = displayedItems;
    // Show the picker 
    [self presentModalViewController:picker animated:YES];
}

The result:

It is not clear to me why the People picker does not show.

解决方案

Curiously what I suggested on Can I use segues with designated initializers of view controllers? fixed the issue for me as well. So creating a proxy ViewController for the ABPeoplePickerNavigationController fixes the issue, but it does not explain, why the built-in pickers can't be used in storyboards:

This is the code for my wrapper class:

#import "PeoplePickerViewControllerWrapper.h"

@implementation PeoplePickerViewControllerWrapper

@synthesize ppvc = _ppvc;  // This is the object I'm proxying (The proxyee so to speak)
@synthesize delegate = _delegate;

- (void)awakeFromNib
{
        self.ppvc = [[ABPeoplePickerNavigationController alloc] init ];
        self.ppvc.peoplePickerDelegate = self;
        self.ppvc.addressBook = ABAddressBookCreate(); 
        self.ppvc.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
}

#pragma mark - View lifecycle

- (void)loadView
{
    [super loadView];
    [self.ppvc loadView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.ppvc viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.ppvc viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.ppvc viewDidDisappear:animated];
}

-(UIView *)view{
    return self.ppvc.view;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [self.ppvc viewDidUnload];
}

这篇关于使用storyboard segue显示ABPeoplePickerNavigationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆