我的问题是,如何让以下自定义展开转场在 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;
}
在 iOS 9 中,segueForUnwindingToViewController
已弃用。它仍然适用于 MyViewController CustomSegue;但是,默认的展开转场不再适用于任何其他展开转场。尽管调用 super 上的方法会返回展开转场,但转场永远不会发生,视图控制器永远不会弹出,并且用户永远无法返回到上一个屏幕。因此,需要明确的是,如果我使用常规的 show segue,则相应的 unwind segue 会调用已弃用的方法,该方法会调用 super 上的方法,并且不起作用。
我观看了 WWDC 关于 Storyboard 的演讲,并且我能够在 iOS 9 中解决这个问题,方法是:a) 不再在我的自定义导航控制器中重写此方法,b) 进入 Storyboard,单击自定义 Segue,然后输入CustomSegue
作为 Segue 类。不幸的是,由于我的目标是 iOS 7,我收到警告“只有自定义转场支持 iOS 9 之前的类名”,并且自定义展开转场现在不适用于 iOS 7 或 iOS 8!
经过多次尝试和错误,我找到了解决方法。我注意到当你重写时segueForUnwindingToViewController
, unwindForSegue:towardsViewController
不再被调用,这就是问题所在。仅供参考,Apple 在 UIViewController 中的注释segueForUnwindingToViewController
says:
已弃用。返回一个 Segue,它将通过以下方式从源视图控制器展开到目标视图控制器unwindForSegue:towardViewController:
方法。当执行故事板中定义的展开序列时,如果展开路径上的任何视图控制器已覆盖此方法并返回非零,运行时将使用该 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;
}
UPDATE:呼唤[super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
似乎仍然适用于模态展开转场。我描述的问题似乎发生在演出转场中。因此,如果您的设备运行版本 [super unwindForSegue:segue towardsViewController:toViewController];当 segue 是模态的时,segue 将不会工作/发生。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)