从自定义模态转换中退出

时间:2014-05-29 10:48:45

标签: objective-c uikit uiviewanimationtransition

我使用自定义过渡进行模态segue,使用UIViewControllerAnimatedTransitioning委托实现。所有segues都通过Storyboard设置。

我有两个导航控制器,都有单根VC设置。我们称他们为1st NVC1st VC2nd NVC2nd VC

1st VC采用UIViewControllerAnimatedTransitioning协议,实现只返回animator:

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    return [ModalTransitionAnimator new];
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [ModalTransitionAnimator new];
}

当任何segue从1st VC发生时 - &gt; 2nd NVC1st VC捕获prepareForSegue:中的事件并将transitioningDelegate与modalPresentationStyle一起分配,因为您无法从IB执行此操作:

UINavigationController* destination = (UINavigationController*)segue.destinationViewController;
destination.transitioningDelegate = self;
destination.modalPresentationStyle = UIModalPresentationCustom;

所以一切看起来都很好,我得到一个非常酷的动画,目的地控制器出现,所有viewWillAppear:viewDidAppear:按照正确的顺序激活。

现在屏幕上,我2nd NVC里面有2nd VC

点击导航栏中的“取消”按钮,在1st VC上展开动作触发器,但不会发生向后动画。我的意思是事实上2nd VC仍在屏幕上。

显然没有人调用dismissViewControllerAnimated。我认为Storyboard很困惑,无法找到返回的方法,因为2nd NVC最初是以模态方式呈现的,然后其他一些控制器请求展开。你是如何正常处理的?

1 个答案:

答案 0 :(得分:1)

我跟着展开链,发现UIKit在状态恢复后无法找到合适的展开区域。在状态恢复之前,一切都很顺利。

我手动修复了它并遍历了我的控制器层次结构,以返回一个定义了展开动作的控制器:

- (UIViewController*)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender {
    if([NSStringFromSelector(action) hasPrefix:@"unwindToInitialViewController"]) {
        return [[((UINavigationController*)self.centerViewController) viewControllers] firstObject];
    }

    UIViewController* c = [super viewControllerForUnwindSegueAction:action fromViewController:fromViewController withSender:sender];
    DDLogInfo(@"%s = %@, c = %@", __PRETTY_FUNCTION__, NSStringFromSelector(action), c);

    return c;
}