小编典典

无法使用情节提要自定义实例化窗口控制器

swift

尝试从情节提要中自定义实例化窗口控制器时,我得到的感觉像是个错误。我正在使用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起是新功能,但文档说它应适用于窗口控制器和视图控制器,并且该错误消息对我没有任何意义。


阅读 244

收藏
2020-07-07

共1个答案

小编典典

我遇到了同样的问题,我想了一下,这就是我的解决方法。

首先,为什么我需要这样做?我想在从Storyboard构建视图控制器层次结构之前向其注入一些依赖项。我想这就是API的目的。但是然后,该方法是否可行,我将如何将注入信息传递到视图控制器层次结构中?

因此,由于该方法在视图控制器中运行时没有错误,因此我决定直接在根视图控制器中注入信息。

所以,我在情节提要中有:

  • 一个名为“ my-window-controller”的窗口控制器场景,该窗口仅指向一个空的视图控制器。
  • 一个名为“ root-view-controller”的视图控制器场景,其中描述了所有视图层次结构。

无论我要创建该视图控制器的什么地方,我都可以做:

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)
    }
}
2020-07-07