• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 支持自定义和默认展开 segues

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:46:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我已经使用 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap