如何在 iOS8 的自定义过渡动画中 "pass" 永久标签?

How can I "pass" a persistent label in a custom transition animation in iOS8?

我有一个视图控制器之间的自定义过渡动画,我希望 UILabel 在 fromViewController 和 toViewController 上是(或出现)相同的。

我尝试了以下方法:

toViewController.nameLabel = fromViewController.nameLabel;

在下面代码的上下文中出现以下错误:

[UINavigationController nameLabel]: unrecognized selector sent to instance

我做错了什么?

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    // Grab the from and to view controllers from the context
    HC_ExercisePageVC *fromViewController = (HC_ExercisePageVC *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    HC_TimerVC *toViewController = (HC_TimerVC *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    if (self.presenting) {

        fromViewController.view.userInteractionEnabled = NO;

        [transitionContext.containerView addSubview:toViewController.view];

        CGRect startFrame = endFrame;
        startFrame.origin.y += 75;
        toViewController.movingViews.frame = startFrame;

        toViewController.nameLabel = fromViewController.nameLabel;

        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            toViewController.movingViews.frame = endFrame;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    }
    else {

更新: 根据@Gavin 的建议,我将代码替换为:

// Grab the from and to view controllers from the context
HC_ExercisePageVC *fromViewController = (HC_ExercisePageVC *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UINavigationController *toViewControllerNavigation = (id)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
HC_TimerVC * toViewController = (HC_TimerVC *)toViewControllerNavigation.viewControllers.firstObject;

但是当我这样做时出现错误:

-[HC_TimerVC viewControllers]: unrecognized selector sent to instance

我总是对如何处理导航控制器感到困惑...

看起来你的 toViewController 包含在一个 UINavigationController 中,因此它将作为目的地返回。

因此您需要从导航控制器中获取 HC_TimerVC

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    // Grab the from and to view controllers from the context
    HC_ExercisePageVC *fromViewController = (HC_ExercisePageVC *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UINavigationController *toViewControllerNavigation = (id)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    HC_TimerVC * toViewController = toViewControllerNavigation.viewControllers.firstObject;
    ....