我试图将一个视图控制器推到一个已经被推的视图控制器上,如下所示。但是,以某种方式,当我尝试推送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)];
}
最佳答案
问题是addPerson
试图从不是当前控制器的视图控制器中以模态形式呈现。 self
(您的原始视图控制器)不是 Activity 控制器。 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];
}