SimplyKiwi
SimplyKiwi

Reputation: 12444

Children view controllers must have a common parent view controller?

In my app I am attempting to switch views which each is their own XIB while using custom animations to do so. I made a base view controller as a parent view to hold all the code but I just can't seem to get this to work.

I made an IBOutlet for all of the view controllers in my app and in interface builder, I connect the outlets to the proper controller. Each controller loads the proper XIB too so none of that is the issue. The issue is the following changing views code.

This is my code:

-(void)changeViews {
    CGRect frame = self.view.frame;
    frame.origin.x = CGRectGetMaxX(frame);
    theView4.view.frame = frame;

    [self.view addSubview:theView4.view];
    [self addChildViewController:theView4];

    [self transitionFromViewController:theView1 
                      toViewController:theView4 
                              duration:1 
                               options:UIViewAnimationOptionTransitionNone
                            animations: ^{
                                CGRect frame = self.view.frame;
                                theView4.view.frame = frame;
                                frame.origin.x -= frame.size.width;
                                self.view.frame = frame;
                            }
                            completion:completion:nil];

And this is the console crash:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Children view controllers <MyGameViewController: 0x1dd25210> and <Settings: 0x1dd249d0> must have a common parent view controller when calling -[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]'

Does anyone know how to fix this?

Thanks!

Upvotes: 4

Views: 14839

Answers (1)

Rui Peres
Rui Peres

Reputation: 25917

You can use this:

When you use addChildViewController: to add the child view controller to the surrounding one. You also have to notify the child controller by didMoveToViewController: that it has been put into another controller. You can also use transitionFromViewController:toViewController: to exchange one view controller for another, optionally giving an animation.

This was taken from here (I would advise you to fully read the accepted answer).

Long story short, you also need to inform that it did move into that parent view controller.

Upvotes: 6

Related Questions