OGeek|极客世界-中国程序员成长平台

标题: ios - 支持自定义和默认展开 segues [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:46
标题: ios - 支持自定义和默认展开 segues

我已经使用 Storyboard实现了一个很好的界面,我决定我想要一个更漂亮的动画来呈现另一个 View Controller 。为了保持一致,这个动画需要一个 segue 和一个 unwind segue。所以我创建了我的 segue 来支持两者:

@interface FKCircularSegue : UIStoryboardSegue

/// If it's an unwind segue
@property (nonatomic,assign) BOOL unwind;

@property (nonatomic,assign) CGRect frameCircle;

@end

在进行转场和展开时效果很好。为了放松,我实现了这个方法:

- (UIStoryboardSegue *)segueForUnwindingToViewControllerUIViewController *)toViewController fromViewControllerUIViewController *)fromViewController identifierNSString *)identifier
{
    if ([identifier isEqualToString"camera.take.close"])
    {
        // Circular segue!
        FKCircularSegue *segue = [[FKCircularSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];

        segue.frameCircle  = _frameCameraButtonTapped;
        segue.unwind        = YES;

        return segue;
    }

    return [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
}

但是!我注意到所有其他正常的展开segues,从推送和模式(这是一种中央 View Controller )回来都停止工作,很明显,[super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]没有这样做。

奇怪的是,调试器显示它实际上返回了一个 UIStoryboardSegue,但它没有做任何事情,点击一个通常会“弹出”你回到这个 View Controller 的按钮什么也没做。

另一方面,如果我注释掉整个 segueForUnwind... 方法,它就会恢复正常,所以 UIViewController 的默认实现是正确的一个。

简而言之,我的问题是,我怎样才能支持一个自定义展开 segue,然后为其他所有展开 segue 使用默认选项?



Best Answer-推荐答案


嗯,我想通了。

结果表明 super 方法没问题,问题出在我创建的 UINavigationController 子类上,该子类将调用转发到此 View Controller 。

为了解决这个问题,我添加了一个任何 View Controller 都可以实现的协议(protocol),并且基本上询问它是否要处理展开转场,或者它应该由导航 Controller 自动完成。

非常冗长,但我认为它比编辑之前发布的解决方案要好。

FKNavigationController.h

/**
 *  Implement this if you want custom unwind segues
 */
@protocol FKNavigationControllerSegueForwarding <NSObject>

- (BOOL)shouldPerformUnwindSegueFromNavigationControllerWithIdentifierNSString*)identifier fromViewControllerUIViewController*)viewController;

@end

/**
 *  Automatically forwards segue methods
 */
@interface FKNavigationController : UINavigationController

@end

FKNavigationController.m

@implementation FKNavigationController

- (UIStoryboardSegue *)segueForUnwindingToViewControllerUIViewController *)toViewController fromViewControllerUIViewController *)fromViewController identifierNSString *)identifier
{
    if ([toViewController respondsToSelectorselector(shouldPerformUnwindSegueFromNavigationControllerWithIdentifier:fromViewController])
    {
        if ([(id<FKNavigationControllerSegueForwarding>)toViewController shouldPerformUnwindSegueFromNavigationControllerWithIdentifier:identifier fromViewController:fromViewController])
        {
            return [toViewController segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
        }
    }

    return [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
}

@end

更新的 View Controller

#pragma mark - Segue forwarding

- (BOOL)shouldPerformUnwindSegueFromNavigationControllerWithIdentifierNSString *)identifier fromViewControllerUIViewController *)viewController
{
    if ([identifier isEqualToString"camera.take.close"])
    {
        return YES;
    }

    return NO;
}

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
    // Check the FKNavigationControllerSegueForwarding if you want to add more.
    if ([identifier isEqualToString"camera.take.close"])
    {
        // Circular segue!
        FKCircularSegue *segue = [[FKCircularSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];

        segue.frameCircle  = _frameCameraButtonTapped;
        segue.unwind        = YES;

        return segue;
    }

    return [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
}

关于ios - 支持自定义和默认展开 segues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23204006/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4