尝试从情节提要中自定义实例化窗口控制器时,我得到的感觉像是个错误。我正在使用NSStoryboard.instantiateController(identifier:creator:),这是MacOS 10.15中的新功能。有问题的代码块是:
Let mainWindowController = storyboard.instantiateController(identifier: "Analysis Window Controller") { aDecoder in
MainWindowController(coder: aDecoder)
}
我已经成功地将这个确切的代码用于自定义实例化主视图控制器,并将该视图分配给一个新窗口和一个新窗口控制器。很好 我还可以使用老式的方式实例化窗口控制器,而无需使用InstantiateController(identifier :)进行自定义初始化。但是,当我尝试使用以上代码对窗口控制器进行自定义实例化时,最终出现以下错误:
-[NSClassSwapper _createControllerForCreator:coder:]中的断言失败...自定义实例化的控制器必须调用-[super initWithCoder:]
请注意,我的自定义视图控制器类(有效)和我的自定义窗口控制器类MainWindowController(无效)都实现了简单的初始化程序:
required init?(coder: NSCoder) {
super.init(coder: coder)
}
我知道此功能自OS 10.15起是新功能,但文档说它应适用于窗口控制器和视图控制器,并且该错误消息对我没有任何意义。
我遇到了同样的问题,我想了一下,这就是我的解决方法。
首先,为什么我需要这样做?我想在从Storyboard构建视图控制器层次结构之前为其注入一些依赖关系。我想这就是API的目的。但是然后,该方法是否有效,如何将注入信息传递到视图控制器层次结构中?
因此,由于该方法在视图控制器中运行时没有错误,因此我决定直接在根视图控制器中注入信息。
所以,我在情节提要中有:
无论我要创建该视图控制器的什么地方,我都可以做:
func instanciateWindowController(storyboard: NSStoryboard) -> NSWindowController {
// Load the (empty) window controller scene
let wcSceneIdentifier = NSStoryboard.SceneIdentifier("my-window-controller")
let windowController = storyboard.instantiateController(withIdentifier: wcSceneIdentifier)
as! NSWindowController
// Load the root view controller using the creator trick to inject dependencies
let vcSceneIdentifier = NSStoryboard.SceneIdentifier("root-view-controller")
let viewController = storyboard.instantiateController(identifier: vcSceneIdentifier,
creator: { coder in
return MyOwnViewController.init(coder: coder,
text: "Victoire !") // just pass here your injection info
})
// Associate the window controller and the root view controller
windowController.contentViewController = viewController
return windowController
}
与
class MyOwnViewController: MSViewController {
init?(coder: NSCoder,
text: String) { // receive here the injection information
print(text) // use the injection information here
super.init(coder: coder)
}
// Not used, but required
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句