ios - 自定义 segue 到不同的 Storyboard

标签 ios objective-c iphone xcode swift

问题:

如何编写自定义 segue 以允许您从不同的 Storyboard嵌入 View Controller ?

上下文:

我正在尝试编写自定义 segue,我可以使用它从一个 Storyboard链接到另一个 Storyboard。一篇关于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)
    }
}

问题:

我在使用他们的 Obj-C 和上面的 Swift 代码时遇到的问题是,当您尝试通过容器 View 使用时(具有嵌入转场的语义 - 从嵌入转场开始,删除转场,然后使用上面的自定义 segue),它在调用 segue 代码之前崩溃,并出现以下未找到方法的错误:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIStoryboardSegueTemplate 0x7ffc8432a4f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key containerView.'

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

总结:

我的最终目标是嵌入在单独的 Storyboard中定义的 View Controller ,以促进协作和测试,而无需编写额外的代码(非侵入性解决方案)。有没有人对如何完成这项更伟大的任务有任何见解?我可以回退到调用 performSegue 的混合方法,但我想找到一个单一的、包含的和完整的解决方案。上面的代码用于事件驱动(按钮等)转场,但不适用于嵌入转场。

感谢任何意见,提前致谢。

最佳答案

您的方法适用于自定义转场以模态方式推送/显示其他 View Controller ,但不适用于嵌入转场。原因是“Embed”segue 不是 UIStoryboardSegue 的子类,而是继承自 UIStoryboardSegueTemplate,它是一个私有(private) API。

不幸的是,我找不到比混合方法更好的方法来实现您想要的。

关于ios - 自定义 segue 到不同的 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26189950/

相关文章:

android - 谷歌 Material 颜色还是自己的颜色?

ios - NSTimer 毫秒倒计时

objective-c - Objective-C - 错误 : Thread 1: EXC_BAD_ACCESS code=2

objective-c - 创建一个包含多个 NSMutableDictionary 对象的 NSMutableArray

iPhone HTTP 请求安全

ios - Apple 的自然语言框架中的词形还原不佳

ios - Swift 使用 realm.add() 或 realm.create() 将大量数据写入 Realm

iphone - 谷歌地图在 Iphone 中的实现

iOS 位置管理器在手机锁定约 10 分钟后停止

iphone - 如何知道一个数字是否大于iphone objective-c中的最大值