出现此crash,基本是因为子视图控制器的切换,也就是这句代码:
transitionFromViewController: toViewController:
且基本是因为fromViewController与toViewController是同一个ViewController,造成Unbalanced
,大多场景是因为加入了segmentControl,控制子视图的切换.以下是示例代码:
- (void)itemChange:(UISegmentedControl *)sender {
switch (sender.selectedSegmentIndex) {
case 0:
{
[self replaceFromOldViewController:currentVC toNewViewController:taskVC];
}
break;
case 1:
{
[self replaceFromOldViewController:currentVC toNewViewController:classVC];
}
break;
case 2:
{
[self replaceFromOldViewController:currentVC toNewViewController:weakVC];
}
break;
default:
break;
}
}
- (void)replaceFromOldViewController:(UIViewController *)oldVc toNewViewController:(UIViewController *)newVc{
if (currentVC == newVc) {//解决上述问题的判断
return;
}
if (newVc != nil) {
[self addChildViewController:newVc];
[self transitionFromViewController:oldVc toViewController:newVc duration:0.1 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
if (finished) {
[newVc didMoveToParentViewController:self];
[oldVc willMoveToParentViewController:nil];
[oldVc removeFromParentViewController];
currentVC = newVc;
}else{
currentVC = oldVc;
}
}];
}
}