gpt4 book ai didi

ios - 自定义 segue 到不同的 Storyboard

转载 作者:IT王子 更新时间:2023-10-29 05:36:57 26 4
gpt4 key购买 nike

问题:

如何编写自定义 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com