• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:06:54 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在尝试使用自定义 segue 从作为子级添加到另一个 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)prepareForSegueMyCustomSegue *)segue senderid)sender
{
    segue.isPresenting = YES;
}

- (UIViewController *)viewControllerForUnwindSegueActionSEL)action fromViewControllerUIViewController *)fromViewController withSenderid)sender
{
    return self;
}

- (UIStoryboardSegue *)segueForUnwindingToViewControllerUIViewController *)toViewController fromViewControllerUIViewController *)fromViewController identifierNSString *)identifier
{
    return [[MyCustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];
}

- (IBAction)unwindToFirstVCUIStoryboardSegue *)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 成为处理 unwind segue Action 的 Controller ,这是不可能的(因为容器 View Controller 只询问它的 children 来处理 unwind segue。

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

谢谢!



Best Answer-推荐答案


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

- (void)perform {
    if (_isPresenting) {
        NSLog(@"resenting");
        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 Action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23911404/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap