Swift:[NSNib\u initWithNibNamed:bundle:options:]无法加载nibName
我正在为生产构建一个Cocoa应用程序,当我创建一个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
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
}