我在视图控制器之间有一个自定义过渡动画,我希望在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]:无法识别的选择器发送到实例
我总是对如何处理导航控制器感到困惑......
答案 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;
....