iOS 8和iOS 9的自定义展开序列

iOS 8和iOS 9的自定义展开序列,ios,ios9,xcode7,backwards-compatibility,unwind-segue,Ios,Ios9,Xcode7,Backwards Compatibility,Unwind Segue,我的问题是,如何让以下自定义展开序列在iOS 9之前版本的设备以及运行iOS 9的设备上工作? 我有一个显示视图控制器的自定义序列,然后有一个相应的自定义展开序列。这段代码在iOS 8中运行良好,通过创建UIStoryboardSegue的子类并实现perform方法来实现。然后在自定义导航控制器中重写以下方法: - (UIStoryboardSegue *) segueForUnwindingToViewController: (UIViewController *)toViewCont

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

我有一个显示视图控制器的自定义序列,然后有一个相应的自定义展开序列。这段代码在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;
}
- (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;
}
在iOS 9中,
segueforunbindingtoviewcontroller
不推荐使用。它仍然适用于MyViewController CustomSegue;但是,默认的展开顺序不再适用于任何其他展开顺序。尽管在super上调用该方法会返回一个展开segue,但是segue永远不会出现,视图控制器永远不会弹出,用户永远不能返回到上一个屏幕。因此,需要明确的是,如果我使用常规的show segue,相应的unwind segue会调用不推荐的方法,该方法在super上调用该方法,但不起作用


我在故事板上观看了WWDC的演讲,通过a)不再在自定义导航控制器中重写此方法,以及b)进入故事板,单击自定义segue,并作为segue类输入
CustomSegue
,我在iOS 9中修复了此问题。不幸的是,因为我的目标是iOS 7,所以我得到警告“只有自定义segue支持iOS 9之前的类名”,而自定义unwind segue现在不适用于iOS 7或iOS 8

经过反复尝试,我找到了一个解决办法。我注意到,当您覆盖
segueForUnwindingToViewController
时,不再调用
unwindForSegue:towardsViewController
,这就是问题所在。仅供参考,苹果在UIViewController中针对
segueForUnwindingToViewController
的注释中写道:

不赞成。返回一个序列,该序列将通过
unwindForSegue:towardViewController:
方法从源视图控制器展开到目标视图控制器。执行情节提要中定义的展开序列时,如果展开路径上的任何视图控制器已重写此方法并返回非nil,则运行时将使用该序列对象,而不是构造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{
        UIStoryboardSegue *unwindSegue = [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]; //Normal Unwind Segue
        segue = unwindSegue;
    }
    return segue;
}
- (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;
}
-(UIStoryboardSegue*)用于从ViewController(UIViewController*)从ViewController(UIViewController*)从ViewController标识符(NSString*)标识符展开到ViewController的分段
{
UIStoryboardSegue*segue;
if([fromViewController是类的种类:[MyViewController类]]){
segue=[[CustomSegue alloc]initWithIdentifier:标识符源:fromViewController目标:toViewController];//自定义展开segue
}
否则{
如果([[UIDevice currentDevice].systemVersion floatValue]<9.0){
返回[super Segue for Unwinding to ViewController:toViewController fromViewController:fromViewController标识符:标识符];//正常的展开顺序
}
否则{
[super unwindForSegue:segue to wardsviewcontroller:to viewcontroller];
返回零;
}
}
返回序列;
}

更新:调用
[超级顺序,用于从ViewController:从ViewController:从ViewController标识符:标识符取消绑定到ViewController:toViewController]似乎仍然适用于模式展开段。我所描述的问题似乎发生在电视节目中。因此,如果您的设备在<9.0的版本上运行,或者如果segue是模态的,您仍然应该调用旧方法。如果调用
[super unwindForSegue:segue towardsViewController:toViewController]当segue处于模式时,segue将不工作/发生。

我稍微修改了您的代码。
我不必考虑推或模态。 它似乎工作得很好

- (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 ([super respondsToSelector:@selector(unwindForSegue:towardsViewController:)]) {
            [super unwindForSegue:segue towardsViewController:toViewController];
        }
        segue = [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
    }

    return segue;
}