设置NavigationController.Viewcontrollers崩溃(使用自定义转换)

时间:2015-01-17 01:43:48

标签: ios objective-c uinavigationcontroller transition

所以这是我的viewcontroller层次结构:

-RootVC
 -MapVC
  -VC0
  -VC1
  -VC2

应用程序启动时将@ [Root,MapVC]推送到视图控制器 - 一切顺利。 然后,我的目标是在四个主要VC(MapVC,VC0,VC1,VC2)之间进行切换时加载Map(因此,我不想弹出MapVC)。

为此,我使用以下代码:

   -(void)presentViewController:(NSString*)viewControllerIdentifier{
    UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:viewControllerIdentifier];

    if (![[self.navigationController.viewControllers objectAtIndex:[[self.navigationController viewControllers] count]-1] isKindOfClass:[vc class]]){

        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
        if (![[viewControllers lastObject] isKindOfClass:[LLMapViewController class]]) {
        [viewControllers removeLastObject];
        }
        [viewControllers addObject:vc];
        [[self navigationController] setViewControllers:viewControllers animated:YES];
        }
}

-(void)presentViewController0:(NSNotification*)notif{
        [self presentViewController:[notif name]];
}

- 展示地图的方法:

-(void)presentMapViewController:(NSNotification*)notif{
    if (![[self.navigationController.viewControllers objectAtIndex:[[self.navigationController viewControllers] count]-1] isKindOfClass:[LLMapViewController class]]){
        [self.navigationController popViewControllerAnimated:YES];
    }
}

嗯,如果我没有为这个过渡设置动画,那么效果很好。 此外,当我执行以下操作时,一切正常

App Launches:
->Root 
 ->MapVC

ACTIONS: 
0: Select VC0
->Root
 ->MapVC
  ->VC0

1: Select VC1 (Pops VC0 and pushes VC1, as you can see in the code above)
->Root
 ->MapVC
  ->VC1

2: Select MapVC (Pops VC1)
->Root
 ->MapVC

问题是当我尝试推送另一个将MapVC作为主VC的VC时:

3: Select VC0
->Root
 ->Map
....

CRASH!

它没有显示任何一致的错误。 有时它不显示任何错误,有时它会显示如下错误:

Example 1: 
-[NSLayoutConstraint navigationController:animationControllerForOperation:fromViewController:toViewController:]:

Example 2: 
-[UILabel navigationController:animationControllerForOperation:fromViewController:toViewController:]:

但是当我设置navigationcontroller的viewcontrollers时,它总是突出显示该部分。

我的问题:我花了大约8个小时试图解决这个问题。我对如何调试它没有更多的想法。 任何人可以帮助我吗?

P.S。我正在使用自定义转换来推/弹控制器。 这是附加代码:

-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {

    LLMenuAnimation *animationController = [[LLMenuAnimation alloc] initWithNavigationController:self.navigationController];
    switch (operation) {
        case UINavigationControllerOperationPush:
            animationController.type = AnimationTypePush;
            return  animationController;
        case UINavigationControllerOperationPop:
            animationController.type = AnimationTypePop;
            return animationController;
        default:
            NSLog(@"OTHER OPERATION");
            return nil;

    }


} 

-

#pragma mark - Animated Transitioning

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.2;
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    //Get references to the view hierarchy
    UIView *containerView = [transitionContext containerView];
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    if (self.type == AnimationTypePush) {
        //Add 'to' view to the hierarchy with 0.5 scale

        toViewController.view.transform = CGAffineTransformMakeScale(0.5, 0.5);


        [containerView insertSubview:toViewController.view aboveSubview:fromViewController.view];

        //Scale the 'to' view to to its final position
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            //            toViewController.view.frame = fromViewController.view.frame;
            toViewController.view.transform = CGAffineTransformMakeScale(1.0, 1.0);

        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    } else if (self.type == AnimationTypePop) {
        //Add 'to' view to the hierarchy
        [containerView insertSubview:toViewController.view belowSubview:fromViewController.view];

        //Scale the 'from' view down 
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            fromViewController.view.transform = CGAffineTransformMakeScale(0.5, 0.5);

        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    }
}

1 个答案:

答案 0 :(得分:0)

我不相信你可以按照你想要的方式使用UINavigationController。如果我已经理解了你想要做什么,你想要从导航控制器下面替换navigationController的viewControllers属性。

顺便说一句,显示的viewControllers属性是一个副本。这是在documentation

中定义的方式
@property(nonatomic, copy) NSArray *viewControllers

您的代码:

        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
    if (![[viewControllers lastObject] isKindOfClass:[LLMapViewController class]]) {
    [viewControllers removeLastObject];
    }
    [viewControllers addObject:vc];
    [[self navigationController] setViewControllers:viewControllers animated:YES];
    }

不会按照您期望的方式运作。您可以将视图控制器推送和弹出到导航控制器上。您无法以您尝试的方式替换viewControllers。

顺便说一句,您在上面问题的前几行中绘制的视图控制器层次结构是不完整的。层次结构中的导航控制器在哪里?