I am trying to push a view controller upon an already pushed view controller like below. However, somehow when I tried to push the newPersonViewController
, this new controller doesn't get pushed onto the stack. I do not know why Xcode is complaining that I am trying to add a new controller that doesn't belong to the hierarchy. Did I present the ABPeoplePickerViewController
incorrectly? Is this the reason why the newPersonViewController
couldn't get pushed onto the stack?
Message from console:
Warning: Attempt to present on whose view is not in the window hierarchy!
The code:
- (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)];
}
The problem is that addPerson
is trying to present modally from the view controller that's not the current controller. The self
(your original view controller) is not the active controller. The ABPeoplePickerNavigationController
is.
So, first, define a class property to keep track of the picker
:
@property (nonatomic, weak) ABPeoplePickerNavigationController *picker;
And then,
showPeoplePicker
to set that property; andaddPerson
to use that property:Thus:
- (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];
}