适用于iOS 8和iOS 9的自定义Unwind Segue

Jos*_*fni 6 backwards-compatibility ios unwind-segue ios9 xcode7

我的问题是,如何让以下自定义展开segue在iOS 9之前的版本以及运行iOS 9的设备上运行?

我有一个自定义Segue显示一个视图控制器,然后有一个相应的自定义展开Segue.此代码在iOS 8中运行良好,并通过创建UIStoryboardSegue的子类并实现该perform方法来实现.然后我在自定义导航控制器中覆盖以下方法:

- (UIStoryboardSegue *) segueForUnwindingToViewController:    (UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
    UIStoryboardSegue *segue;
    if([fromViewController isKindOfClass:[MyViewController class]]){
        segue = [[CustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController]; //Custom Unwind Segue
    }
    else{
        UIStoryboardSegue *unwindSegue = [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]; //Normal Unwind Segue
        segue = unwindSegue;
    }
    return segue;
}
Run Code Online (Sandbox Code Playgroud)

在iOS 9中,segueForUnwindingToViewController已弃用.它仍适用于MyViewController CustomSegue; 但是,默认的展开segue不再适用于任何其他展开segue.虽然在super上调用方法会返回一个展开segue,但segue永远不会发生,视图控制器永远不会弹出,并且用户永远不会返回到前一个屏幕.所以要清楚一点,如果我使用常规show segue,相应的unwind segue会调用不推荐使用的方法,该方法在super上调用方法,但不起作用.

我在故事板上观看了WWDC的演讲,我能够在iOS 9中修复此问题:a)不再在我的自定义导航控制器中覆盖此方法,b)进入故事板,单击自定义segue,然后输入CustomSegue作为Segue Class.不幸的是,由于我的目标是iOS 7,我收到警告"只有自定义segues支持iOS 9之前的类名",而自定义展开segue现在不适用于iOS 7或iOS 8!

Jos*_*fni 3

经过多次尝试和错误,我找到了解决方法。我注意到,当您覆盖 , 时segueForUnwindingToViewControllerunwindForSegue:towardsViewController不再被调用,这就是问题所在。仅供参考,Apple 在 UIViewController 中的注释segueForUnwindingToViewController说:

已弃用。返回一个 Segue,它将通过该方法从源视图控制器展开到目标视图控制器unwindForSegue:towardViewController:。当执行故事板中定义的展开 Segue 时,如果展开路径上的任何视图控制器已重写此方法并返回非 nil,则运行时将使用该 Segue 对象,而不是构造 Interface Builder 中指定的类的实例。

粗体部分的语句似乎没有被实现,即如果你重写这个方法但返回nil,unwindForSegue:towardViewController仍然没有被调用并且segue也不会发生。

为了解决这个问题,我能够segueForUnwindingToViewController在我的自定义导航控制器中编辑:

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
    UIStoryboardSegue *segue;
    if([fromViewController isKindOfClass:[MyViewController class]]){
        segue = [[CustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController]; //Custom Unwind Segue
    }
    else{
        if([[UIDevice currentDevice].systemVersion floatValue] < 9.0){
            return [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]; //Normal Unwind Segue
        }
        else{
            [super unwindForSegue:segue towardsViewController:toViewController];
            return nil;
        }
    }
    return segue;
}
Run Code Online (Sandbox Code Playgroud)

更新:调用[super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];似乎仍然适用于模式展开转场。我描述的问题似乎发生在表演片段中。因此,如果您的设备运行版本 < 9.0 或者如果 segue 是模态的,您仍然应该调用旧方法。[super unwindForSegue:segue towardsViewController:toViewController];如果您在 Segue 为模态时调用,则 Segue 将不会工作/发生。