适用于iOS 8和iOS 9的自定义Unwind Segue [英] Custom Unwind Segue for iOS 8 and iOS 9

查看:191
本文介绍了适用于iOS 8和iOS 9的自定义Unwind Segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

I have a Custom Segue showing a view controller, and then have a corresponding Custom Unwind Segue. This code has worked fine in iOS 8, and is implemented by creating subclasses of UIStoryboardSegue and implementing the perform method. Then I override the following method in my custom Navigation Controller:

- (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;但是,默认的展开segue不再适用于任何其他展开segue。尽管在super上调用方法会返回一个展开segue,但segue永远不会发生,视图控制器永远不会弹出,用户永远不会返回到前一个屏幕。所以要清楚一点,如果我使用常规show segue,相应的unwind segue会调用不推荐使用的方法,该方法在super上调用方法,但不起作用。

In iOS 9, segueForUnwindingToViewController is deprecated. It still works for the MyViewController CustomSegue; however, the default unwind segue no longer works for any other unwind segue. Although calling the method on super returns an unwind segue, the segue never occurs, the view controller is never popped, and the user can never go back to the previous screen. So just to be clear, if I use a regular show segue, the corresponding unwind segue calls the deprecated method, which calls the method on super, and does not work.

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

I watched the WWDC talk on storyboards, and I was able to fix this issue in iOS 9 by a) no longer overriding this method in my custom Navigation Controller, and b) going into the storyboard, clicking on the custom segue, and entering in CustomSegue as the Segue Class. Unfortunately, since I am targeting iOS 7, I get the warning "Only Custom segues support class names prior to iOS 9", and the custom unwind segue now does not work for iOS 7 or iOS 8!

推荐答案

经过大量的反复试验后,我找到了解决方法。我注意到当你覆盖 segueForUnwindingToViewController 时,不再调用 unwindForSegue:towardsViewController ,这就是问题所在。仅供参考,Apple在UIViewController中为提供的注释:segueForUnwindingToViewController 说:

After a lot of trial and error, I found a workaround. I noticed that when you override segueForUnwindingToViewController, unwindForSegue:towardsViewController is no longer called, which is the problem. FYI, Apple's note in UIViewController for segueForUnwindingToViewController says:


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

Deprecated. Returns a segue that will unwind from the source to destination view controller via the unwindForSegue:towardViewController: method. When performing an unwind segue defined in a storyboard, if any view controller along the unwind path has overridden this method and returns non-nil, the runtime will use that segue object instead of constructing an instance of the class specified in Interface Builder.

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

The portion of the statement in bold does not seem to be implemented, i.e. if you override this method but return nil, unwindForSegue:towardViewController is still not called and the segue does not occur.

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

In order to get around this problem, I was able to edit the segueForUnwindingToViewController in my custom Navigation Controller:

- (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;
}

更新:致电 [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]; 似乎仍适用于模态展开segues。我描述的问题似乎发生在show segues上。因此,如果您的设备运行版本< 9.0或者如果segue是模态的,你仍然应该调用旧方法。如果你调用 [super unwindForSegue:segue towardsViewController:toViewController]; 当segue是模态时,segue将无法工作/发生。

UPDATE: Calling [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]; seems to still work for modal unwind segues. The issue I described seems to happen for show segues. Consequently, if your device runs on a version < 9.0 or if the segue is modal, you should still call the old method. If you call [super unwindForSegue:segue towardsViewController:toViewController]; when the segue is modal, the segue will not work/occur.

这篇关于适用于iOS 8和iOS 9的自定义Unwind Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆