【问题标题】:Container View Controller cannot handle Unwind Segue Action容器视图控制器无法处理 Unwind Segue Action
【发布时间】:2014-07-17 15:56:08
【问题描述】:

我在尝试使用自定义 segue 从作为子级添加到另一个视图控制器的视图控制器中展开时遇到问题。

这是 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");
}

所有必要的连接也都在情节提要中完成。

我的问题是 -segueForUnwindingToViewController: 永远不会被调用。 只要返回-viewControllerForUnwindSegueAction:fromViewController:withSender:,我的程序就会崩溃,并出现以下异常:

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

据我了解,崩溃的原因是我希望我的容器视图控制器成为处理 unwind segue 动作的控制器,这是不可能的(因为容器视图控制器只询问它的 children 来处理 unwind segue。

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

谢谢!

【问题讨论】:

  • 所以基本上你想要的是回到以前的控制器,不是吗?
  • 是的,我想从 secondVC 返回到 firstVC
  • 我在下面发布了一个答案。请检查并通知我。

标签: ios ios6 segue uicontainerview parentviewcontroller


【解决方案1】:

我认为你不能那样使用 unwind segue(至少我找不到方法)。相反,您可以创建另一个从子控制器返回到父控制器的“正常”segue,并将其类型设置为自定义,使用与从第一个控制器到第二个控制器相同的类。在第二个控制器的 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];
        }];
    }
}

【讨论】:

    【解决方案2】:

    如果您想从 SecondVC 返回到 FirstVC,请尝试在您告诉控制器返回到前一个控制器的位置使用此代码。

    [self dismissViewControllerAnimated:YES completion:nil];
    

    这将起作用,您不需要展开代码。

    希望对您有所帮助。

    【讨论】:

    • 不,它不起作用。我没有展示 SecondVC,我将它作为一个孩子添加到第一个视图控制器中,所以我的工作是使用我的自定义 segue 将其关闭...
    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 2021-10-26
    • 2015-06-14
    • 1970-01-01
    相关资源
    最近更新 更多