使用storyboard segue显示ABPeoplePickerNavigationController

时间:2012-01-21 12:49:01

标签: objective-c ios cocoa uistoryboard abpeoplepickerview

我有一个新项目,我想在触摸按钮时显示人物选择器。

所以我有一个UIButton,它使用标识符UIViewController来转换为通用showContacts。我将此ViewController的类设置为ABPeoplePickerNavigationController

现在在我的根ViewController中我有这个代码来初始化我的选择器:

#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]];
    }
}

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

no picker http://i.minus.com/jbwUQyLr36ChHo.png

使用以下代码(与prepareForSegue:方法中的代码非常相似),我设法通过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];
}

结果:

picker http://i.minus.com/jeEVeIBmfIYdR.png

我不清楚为什么人物选择器没有显示。

3 个答案:

答案 0 :(得分:5)

贝西的回答很棒。但是,使用旧方法而不是使用故事板来减少代码:

- (void)showPeoplePicker:(id)sender
{
    ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    picker.modalPresentationStyle = UIModalPresentationFullScreen;
    picker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:picker
                       animated:YES                     
                     completion:^{
                     // animation to show view controller has completed.
                     }];
}

答案 1 :(得分:3)

奇怪的是,我在Can I use segues with designated initializers of view controllers?上的建议也为我解决了这个问题。因此,为ABPeoplePickerNavigationController创建代理ViewController解决了这个问题,但它没有解释为什么内置的选择器无法在故事板中使用:

这是我的包装类的代码:

#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];
}

答案 2 :(得分:0)

正如马特所说,好的方法还可以。如果您想使用storyboard,可以在自定义视图控制器中添加ABPeoplePickerNavigationController,如下所示:

- (void)awakeFromNib
{    
    ABPeoplePickerNavigationController * peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
    peoplePicker.peoplePickerDelegate = self;
    // Display only a person's phone and address
    NSArray * displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonAddressProperty],
                            [NSNumber numberWithInt:kABPersonPhoneProperty],
                            nil];

    peoplePicker.displayedProperties = displayedItems;

    [self.view addSubview:peoplePicker.view];
    [self addChildViewController:peoplePicker];
    [peoplePicker didMoveToParentViewController:self];    
}