ios - 容器 View Controller 无法处理 Unwind Segue 操作

标签 ios ios6 segue uicontainerview parentviewcontroller

我在尝试使用作为子级添加到另一个 View Controller 的 View Controller 中的自定义转场进行展开时遇到问题。

这是 MyCustomSegue.m:

- (void)perform
{
    if (_isPresenting)
    {
        //Present
        FirstVC *fromVC = self.sourceViewController;
        SecondVC *toVC = self.destinationViewController;

        toVC.view.alpha = 0;

        [fromVC addChildViewController:toVC];
        [fromVC.view addSubview:toVC.view];

        [UIView animateWithDuration:1.0 animations:^{

            toVC.view.alpha = 1;
            //fromVC.view.alpha = 0;

        } completion:^(BOOL finished){

            [toVC didMoveToParentViewController:fromVC];

        }];

    }
    else
    {
        //Dismiss
    }
}

这是我的 FirstVC.m:

- (void)prepareForSegue:(MyCustomSegue *)segue sender:(id)sender
{
    segue.isPresenting = YES;
}

- (UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender
{
    return self;
}

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
    return [[MyCustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];
}

- (IBAction)unwindToFirstVC:(UIStoryboardSegue *)segue
{
    NSLog(@"I am here");
}

所有必要的连接也在 Storyboard中完成。

我的问题是-segueForUnwindingToViewController:从未被调用过。 一旦-viewControllerForUnwindSegueAction:fromViewController:withSender:返回后,我的程序崩溃并出现以下异常:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not find a view controller to execute unwinding for <FirstViewController: 0x8e8d560>'

据我了解,崩溃的原因是我希望我的容器 View Controller 能够处理展开转场操作,这是不可能的(因为容器 View Controller 仅询问其子级 处理展开转场。

正确吗? 我可以做什么来解决我的问题?

谢谢!

最佳答案

我认为你不能以这种方式使用展开转场(至少我找不到办法)。相反,您可以创建另一个从子 Controller 返回到父 Controller 的“正常”segue,并将其类型设置为自定义,并使用与从第一个 Controller 到第二个 Controller 相同的类。在第二个 Controller 中的prepareForSegue中,将isPresenting设置为NO,并将此代码放入您的自定义segue中,

- (void)perform {
    if (_isPresenting) {
        NSLog(@"Presenting");
        ViewController *fromVC = self.sourceViewController;
        SecondVC *toVC = self.destinationViewController;
        toVC.view.alpha = 0;
        [fromVC addChildViewController:toVC];
        [fromVC.view addSubview:toVC.view];

        [UIView animateWithDuration:1.0 animations:^{

            toVC.view.alpha = 1;

        } completion:^(BOOL finished){
            [toVC didMoveToParentViewController:fromVC];
        }];

    }else{
        NSLog(@"dismissing");
        SecondVC *fromVC = self.sourceViewController;
        [fromVC willMoveToParentViewController:nil];
        [UIView animateWithDuration:1.0 animations:^{
            fromVC.view.alpha = 0;
        } completion:^(BOOL finished) {
            [fromVC removeFromParentViewController];
        }];
    }
}

关于ios - 容器 View Controller 无法处理 Unwind Segue 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23911404/

相关文章:

ios - iOS 9.2 上的游戏中心崩溃应用程序

ios - 在 iOS 6 中启用自动布局,同时保持向后兼容 iOS 5

iphone - 是否可以更改 PKAddPassesViewController 的标题?

ios - 在 ios6 中更改导航栏按钮项目颜色

ios - 如何从 Viewcontroller 转到 UITableViewCell

ios - 是否可以使用从单个模态视图 Controller 返回到同一源 View Controller 的多个实例之一的展开转场?

iOS:阐明不同的搜索路径

ios - 带侧面菜单的页面查看器(抽屉导航)

ios - 后台方法停止在设备上运行,仅适用于模拟器swift 4 xcode 9

swift - 将一些数据传回 Launcher View Controller