我正在尝试将视图控制器推到已推送的视图控制器上,如下所示。但是,当我试图推动newPersonViewController
时,这个新控制器不会被推入堆栈。我不知道为什么Xcode抱怨我试图添加一个不属于层次结构的新控制器。我是否错误地展示了ABPeoplePickerViewController
?这就是newPersonViewController
无法被推入堆栈的原因吗?
来自控制台的消息:
警告:尝试显示其视图不在窗口层次结构中!
代码:
- (void)addPerson
{
ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init];
newPersonVC.newPersonViewDelegate = self;
UINavigationController *uiNav = [[UINavigationController alloc] initWithRootViewController:newPersonVC];
[self presentViewController:uiNav animated:YES completion:nil];
}
- (void)showPeoplePicker
{
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
[picker.topViewController setEditing:YES];
picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];
picker.topViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(peoplePickerCancel)];
}
答案 0 :(得分:1)
问题是addPerson
试图从不是当前控制器的视图控制器模态呈现。 self
(您的原始视图控制器)不是活动控制器。 ABPeoplePickerNavigationController
是。
首先,定义一个类属性来跟踪picker
:
@property (nonatomic, weak) ABPeoplePickerNavigationController *picker;
然后,
showPeoplePicker
以设置该属性;和addPerson
以使用该属性:因此:
- (void)addPerson
{
ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init];
newPersonVC.newPersonViewDelegate = self;
UINavigationController *uiNav = [[UINavigationController alloc] initWithRootViewController:newPersonVC];
[self.picker presentViewController:uiNav animated:YES completion:nil];
}
- (void)showPeoplePicker
{
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
[picker.topViewController setEditing:YES];
picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];
picker.topViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(peoplePickerCancel)];
self.picker = picker;
}
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person
{
[newPersonView dismissViewControllerAnimated:YES completion:nil];
}