Swift:[NSNib\u initWithNibNamed:bundle:options:]无法加载nibName

Swift:[NSNib\u initWithNibNamed:bundle:options:]无法加载nibName,swift,cocoa,Swift,Cocoa,我正在为生产构建一个Cocoa应用程序,当我创建一个NSViewController用于路由,而没有NSStoryboard实例化时,我得到如下错误 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: ContentFully.Anot

我正在为生产构建一个Cocoa应用程序,当我创建一个
NSViewController
用于路由,而没有
NSStoryboard
实例化时,我得到如下错误

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: ContentFully.AnotherController in bundle (null).
实际上,我通过使用
NSViewController
添加
NSStoryboard
解决了我的问题,但我想了解当我以编程方式调用它时发生了什么,以及为什么崩溃

工作场景

崩溃情景

即使我完全以编程方式创建一个与
NSStoryboard
无关的类,当我更改
contentViewController
时,我也无法使用它。是否有必要在
NSBundle
中的每个控制器中搜索
Swift


提前感谢

非常简单的工作版本是:

import Cocoa

class AnotherController: NSViewController {

    override func loadView() {
        view = NSView(frame: NSMakeRect(0.0, 0.0, 400.0, 270.0))
        let label = NSTextField(labelWithString: "Another Controller")
        view.addSubview(label)
    }
    
}
我可以使用您所说的代码从我的第一个控制器调用它,并得到预期的结果

@IBAction func replaceMe(_ sender: Any) {
    let vc = AnotherController()
    self.view.window?.contentViewController = vc
}

感谢您的回答,但我已经尝试了所有创建类的方法(以编程方式,通过重写
loadView()
,或者只实现自定义的
init()
方法)。如果可能的话,你能提供一个为我的情况创建控制器的例子吗?@elia Answer改为显示代码。(显然,它需要更多的工作来调整大小和限制。最后,故事板实现可能会更简单。)是的,谢谢Phillip。我了解到,当我使用
NSStoryboard
+
编程方法时,我必须使用
loadView
以编程方式使用。
import Cocoa

class AnotherController: NSViewController {

    override func loadView() {
        view = NSView(frame: NSMakeRect(0.0, 0.0, 400.0, 270.0))
        let label = NSTextField(labelWithString: "Another Controller")
        view.addSubview(label)
    }
    
}
@IBAction func replaceMe(_ sender: Any) {
    let vc = AnotherController()
    self.view.window?.contentViewController = vc
}