I am using view controller containment to transition between 6 view controllers. Transitions are controlled using a segmented control. This all works fine unless buttons on the segmented control are pushed before animation of the previous transition has completed. In this situation, the app crashes with
'Children view controllers and must have a common parent view controller when calling -[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]'
Code is:
[self transitionFromViewController:currentVC
toViewController:newVC
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:nil
completion:^(BOOL finished) {
[currentVC removeFromParentViewController];
[newVC didMoveToParentViewController:self];
currentVC = newVC;
}];
Should I disable the segmented control until animation is completed? Or is their a better way to avoid this issue?
You can disable and reenable application interaction by calling
when the animation starts and ends respectively. The app will then ignore all interaction (touch events) until the animation is finished, so the segment will never receive event before it is safe (animations are done).
I think this approach is used on some built-in container controllers as well. However be careful about animation duration. If the animation will take a long time, it can look like the app is not responding well, which hurts user experience