我怎样才能通过" iOS8中自定义过渡动画中的持久标签?

时间:2015-03-02 23:03:05

标签: uiviewcontroller ios8 transitions

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

我尝试了以下内容:

toViewController.nameLabel = fromViewController.nameLabel;

在下面的代码中,但出现以下错误:

  

[UINavigationController nameLabel]:发送到的无法识别的选择器   实例

我做错了什么?

- (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]:无法识别的选择器发送到实例

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

1 个答案:

答案 0 :(得分:0)

您的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;
    ....