objective-cuiviewcontrolleruinavigationcontrollerchildviewcontrollerparentviewcontroller

transitionFromViewController:toViewController error: children view controllers must have a common parent view controller


I'm trying to transition from my current view controller self to self.aVC using view controller containment using self.navigationController as the container. When I run the following code, the "children view controllers must have a common parent view controller" error pops up but the two NSLogs show the same parentViewController.

What seems to be the problem here? Any help is appreciated.

[self willMoveToParentViewController:nil];
[self.navigationController addChildViewController:self.aVC];
[self.aVC.view setFrame:self.bottomFrame];

NSLog(@"%@",self.parentViewController);
NSLog(@"%@",self.aVC.parentViewController);

//    __weak __block SBSomeVC *weakSelf = self;
[self transitionFromViewController:self toViewController:self.aVC duration:0.3 options:UIViewAnimationOptionTransitionNone animations:^{
    [self.aVC.view setFrame:self.view.bounds];
} completion:^(BOOL finished) {
    [self.aVC didMoveToParentViewController:self.navigationController];
    [self removeFromParentViewController];
}];

Solution

  • To my understanding, -transitionFromViewController:toViewController:duration: must be called on a given view controller to transition between two child view controllers.

    So, in your case, you should move that method call in the parent view controller class.

    You should also ensure that both the view controllers you pass as arguments are already added as children view controllers of the parent one, or you'll keep getting the same error.

    Reference: https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html