如何在Swift/Cocoa中将NSNib.Name传递给窗口控制器
在过去的一年半里,斯威夫特/科科发生了什么事,这让我的旧大脑陷入了困境。首先,我了解到windowNibName现在是一个结构而不是字符串,感谢@technerd在我的MainWindowController类中为我解决了这个问题:如何在Swift/Cocoa中将NSNib.Name传递给窗口控制器,swift,cocoa,Swift,Cocoa,在过去的一年半里,斯威夫特/科科发生了什么事,这让我的旧大脑陷入了困境。首先,我了解到windowNibName现在是一个结构而不是字符串,感谢@technerd在我的MainWindowController类中为我解决了这个问题: override var windowNibName: NSNib.Name? { return NSNib.Name( "MainWindowController" ) } 但是,如何使用呢 我的AppDelegate类中用于工作的以下代码: var m
override var windowNibName: NSNib.Name? {
return NSNib.Name( "MainWindowController" )
}
但是,如何使用呢
我的AppDelegate类中用于工作的以下代码:
var mainWindowController: MainWindowController?
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
// Create a window controller with a XIB file of the same name
let mainWindowController = mainWindowController()
// Put the window of the window controller on screen
mainWindowController.showWindow( self )
// Set the property to point to the window controller
self.mainWindowController = mainWindowController
}
但后来我从Xcode中得到了大量的错误和有用的解决方案,我很高兴从一开始就遵循这些解决方案
在其自身初始值内使用的变量;使用“self.”来指代
变量
直到我被留下:
var mainWindowController: MainWindowController?
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
// Create a window controller with a XIB file of the same name
let mainWindowController = self.mainWindowController
// Put the window of the window controller on screen
mainWindowController?.showWindow( self )
// Set the property to point to the window controller
self.mainWindowController = mainWindowController
}
这不起作用——它编译得很好,但没有窗口显示。这有点道理,因为我猜这个新的mainWindowController不知道传递的是什么。但是,尽管我可能会修修补补,我还是不能让它工作。任何想法都是非常受欢迎的
提前感谢,,
Craig你把小写和大写符号混在一起了。如果类和变量使用相同的名称,请注意如何编写,区分大小写很重要 要初始化类,必须使用大写名称
self.mainWindowController = MainWindowController()
mainWindowController?.showWindow( self )
哦,天哪,@vadian,我是个白痴。在这里,我认为这是新的东西