不能使用故事板自定义实例化窗口控制器

Tyl*_*son 9 macos nswindowcontroller xcode-storyboard swift macos-catalina

尝试从故事板自定义实例化窗口控制器时,我感觉像是一个错误。我正在使用NSStoryboard.instantiateController(identifier:creator:),这是 MacOS 10.15 的新功能。有问题的代码块是:

let mainWC = storyboard.instantiateController(identifier: "id") { aDecoder in  
    MainWindowController(coder: aDecoder)  
} 
Run Code Online (Sandbox Code Playgroud)

我基本上成功地使用了这个确切的代码来自定义实例化主视图控制器,并将该视图分配给一个新窗口和一个新窗口控制器。这很好用。我还可以以老式的方式实例化窗口控制器,而无需使用instantiateController(identifier:). 但是当我尝试上面的代码来自定义窗口控制器的实例化时,我最终遇到了以下错误:

断言失败-[NSClassSwapper _createControllerForCreator:coder:]...自定义实例化控制器必须调用-[super initWithCoder:]

请注意,我的自定义视图控制器类(有效)和我的自定义窗口控制器类MainWindowController(无效)都实现了平凡的初始值设定项:

required init?(coder: NSCoder) {  
    super.init(coder: coder)  
}
Run Code Online (Sandbox Code Playgroud)

我知道这个功能是 OS 10.15 的新功能,但文档说它应该适用于窗口控制器和视图控制器,并且错误消息对我没有任何意义。

Air*_*ène 5

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

首先,为什么我需要这个?我想在从 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
}
Run Code Online (Sandbox Code Playgroud)

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)
    }
}
Run Code Online (Sandbox Code Playgroud)