我有一个新项目,我想在触摸按钮时显示人员选择器。
所以我有一个 UIButton
,它转至具有标识符 showContacts
的通用 UIViewController
。我将此 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
我不清楚为什么人员选择器不显示。
最佳答案
Besi 的回答很棒。但是只使用旧方法而不是使用 Storyboard的代码更少:
- (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.
}];
}
关于objective-c - 使用 Storyboard segue 显示 ABPeoplePickerNavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8953080/