大家好,我正在动画的2 UIViewController的显示在一个UIContainerView。对于视图控制器的表示,我实现了这个方法。
#pragma mark - Animation Controller
-(void)moveViewControllerFrom:(UIViewController *)currentViewController toViewController:(UIViewController *)nextViewController {
CGFloat widthVC = self.containerView.frame.size.width;
CGFloat heightVC = self.containerView.frame.size.height;
if (currentViewController == nextViewController) return;
if (_isNextViewController) {
nextViewController.view.frame = CGRectMake(widthVC, 0, widthVC, heightVC);
[self addChildViewController:nextViewController];
[currentViewController willMoveToParentViewController:nil];
[self transitionFromViewController:currentViewController toViewController:nextViewController duration:.4 options:0 animations:^{
nextViewController.view.frame = currentViewController.view.frame;
currentViewController.view.frame = CGRectMake(0 - widthVC, 0, widthVC, heightVC);
}
completion:^(BOOL finished) {
[currentViewController removeFromParentViewController];
[nextViewController didMoveToParentViewController:self];
}];
}
else {
nextViewController.view.frame = CGRectMake(0 -widthVC, 0, widthVC, heightVC);
[self addChildViewController:nextViewController];
[currentViewController willMoveToParentViewController:nil];
[self transitionFromViewController:currentViewController toViewController:nextViewController duration:.4 options:0 animations:^{
nextViewController.view.frame = currentViewController.view.frame;
currentViewController.view.frame = CGRectMake(widthVC, 0, widthVC, heightVC);
}
completion:^(BOOL finished) {
[currentViewController removeFromParentViewController];
[currentViewController didMoveToParentViewController:self];
}];
}
}现在,当我快速地按下前进和后退按钮(用于显示新视图控制器或返回到前一个视图控制器的按钮)时,我遇到了问题。我的应用程序crasha和consol将此错误返回给我:
*终止应用程序,原因是非正常异常“NSInvalidArgumentException”,原因:“子视图控制器,并且在调用transitionFromViewController:toViewController:duration:options:animations:completion:‘时必须有一个公共父视图控制器
有人能帮我理解我的代码哪里错了吗?
发布于 2018-09-25 22:16:45
当您第二次调用currentViewController时,您的moveViewController:toViewController可能还没有完全添加到moveViewController:toViewController中。您可以在方法的开头放置这样的语句,以确定我的假设是否正确:
NSAssert([self.childViewControllers containsObject:currentViewController], @"The currentViewController: %@ is not a childViewController, here are the current childViewControllers: %@", currentViewController, self.childViewControllers);编辑:,如果这个假设不正确,那么编辑您的问题可能有助于将您对该方法的调用包含在进一步的上下文中
https://stackoverflow.com/questions/52499052
复制相似问题