支持自定义和默认展开segues

时间:2014-04-21 19:02:34

标签: ios cocoa-touch uistoryboard uistoryboardsegue

我使用故事板实现了一个很好的界面,我决定想要一个更高级的动画来呈现另一个视图控制器。这个动画要保持一致,需要segue和unwind segue。所以我创建了我的segue以支持两者:

@interface FKCircularSegue : UIStoryboardSegue

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

@property (nonatomic,assign) CGRect frameCircle;

@end

在进行segue和unwinding时效果很好。为了展开,我实现了方法:

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)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];
}

BUT!我注意到所有其他正常的展开segues,从推送和模态回来(这是一种中央视图控制器)停止工作,显然,[super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]没有这样做。

奇怪的是,调试器显示它实际上正在返回一个UIStoryboardSegue,但它没有做任何事情,点击一个通常会“弹回”你回到这个视图控制器的按钮什么都不做。

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

简而言之,我的问题是,如何支持一个自定义展开segue,然后对每个其他展开segue使用默认值?

1 个答案:

答案 0 :(得分:1)

好吧,我想出了这个问题。

结束super方法没问题,问题在于我所做的UINavigationController子类将调用转发给这个视图控制器。

为了修复它,我添加了一个任何视图控制器都可以实现的协议,并且基本上询问它是否要处理展开segue,或者它应该由导航控制器自动完成。

它非常详细,但我认为它比编辑前发布的解决方案更好。

FKNavigationController.h

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

- (BOOL)shouldPerformUnwindSegueFromNavigationControllerWithIdentifier:(NSString*)identifier fromViewController:(UIViewController*)viewController;

@end

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

@end

FKNavigationController.m

@implementation FKNavigationController

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
    if ([toViewController respondsToSelector:@selector(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

更新了视图控制器

#pragma mark - Segue forwarding

- (BOOL)shouldPerformUnwindSegueFromNavigationControllerWithIdentifier:(NSString *)identifier fromViewController:(UIViewController *)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];
}