自定义segue到不同的故事板

Chr*_*ver 10 iphone xcode objective-c ios swift

题:

如何编写一个自定义segue,允许您从不同的故事板嵌入视图控制器?

语境:

我正在尝试编写一个自定义segue,我可以从一个故事板链接到另一个故事板.atomicobject.com上的一篇好文章说明了如何创建一个源自按钮/事件等的segue.转换为swift,并允许非UINavigationControllers,代码如下所示:

public class SegueToStoryboard : UIStoryboardSegue {

    private class func viewControllerInStoryBoard(identifier:String, bundle:NSBundle? = nil)
        -> UIViewController?
    {
        let boardScene = split(identifier, { $0 == "." }, maxSplit: Int.max, allowEmptySlices: false)
        switch boardScene.count {
        case 2:
            let sb = UIStoryboard(name: boardScene[0], bundle: bundle)
            return sb.instantiateViewControllerWithIdentifier(boardScene[1]) as? UIViewController
        case 1:
            let sb = UIStoryboard(name: boardScene[0], bundle: bundle)
            return sb.instantiateInitialViewController() as? UIViewController
        default:
            return nil
        }
    }

    override init(identifier: String!,
        source: UIViewController,
        destination ignore: UIViewController) {
        let target = SegueToStoryboard.viewControllerInStoryBoard(identifier, bundle: nil)
        super.init(identifier: identifier, source: source,
            destination:target != nil ? target! : ignore)
    }

    public override func perform() {
        let source = self.sourceViewController as UIViewController
        let dest = self.destinationViewController as UIViewController
        source.addChildViewController(dest)
        dest.didMoveToParentViewController(source)
        source.view.addSubview(dest.view)

//      source.navigationController?.pushViewController(dest, animated: true)
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:

我用他们的Obj-C和上面的Swift代码的问题是当你尝试使用via一个容器视图时(带有嵌入segue的语义 - 从embed segue开始,删除segue,然后使用上面的自定义segue),它在使用以下方法调用segue代码之前崩溃 - 找不到错误:

***由于未捕获的异常'NSUnknownKeyException'终止应用程序,原因:'[<UIStoryboardSegueTemplate 0x7ffc8432a4f0> setValue:forUndefinedKey:]:此类不是密钥容器视图的密钥值编码兼容.

我试图检查列出的地址,但没有得到有意义的结果.我看到大胆的声明,它期望containerView但不知道如何隔离,满足和/或解决这个问题.

摘要:

我的最终目标是嵌入在单独的故事板中定义的视图控制器,以便于协作和测试,而无需编写额外的代码(非侵入式解决方案).有没有人对如何完成这项更大的任务有任何见解?我可以回到调用performSegue的混合方法,但想找到一个包含的,完整的解决方案.上面的代码可以用于事件驱动(按钮等)segues,但不能用于嵌入segue.

感谢任何意见,提前感谢.

zmi*_*mit 6

您的方法适用于自定义segues以模式方式推送/显示其他视图控制器,但不适用于嵌入segues.原因是"嵌入"segue不是UIStoryboardSegue的子类,而是继承自UIStoryboardSegueTemplate,它是一个私有API.

不幸的是,我找不到比混合方法更好的方法来实现你想要的东西.