使用storyboard segue显示ABPeoplePickerNavigationController
我有一个新项目,我想在触摸按钮时显示人物选择器。
所以我有一个UIButton
,它与一个标识为showContacts
的通用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]]; } }
虽然我已将测试联系人添加到我的模拟器地址簿中,但结果如下所示:
没有选择器http://sofzh.miximages.com/objective-c/jbwUQyLr36ChH.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]; }
结果:
选择器http://sofzh.miximages.com/objective-c/jeEVeIBmfIYdR.png
我不清楚为什么人物选择器没有显示。
贝西的答案很棒。 但是使用旧方法代替使用故事板的代码更少:
- (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. }]; }
奇怪的是我的建议我可以使用segues与视图控制器的指定初始化器吗? 我也解决了这个问题。 因此,为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]; }
正如马特所指出的那样,旧的方式还可以。 如果要使用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]; }