如何在Swift2中从我的Xib文件中打开新窗口?我已经创建了新的NSWindowController + xib,但我找不到如何打开它?
@IBAction func openPreferences(sender: NSMenuItem) {
let wc = SettingsViewController()
//...
}
答案 0 :(得分:2)
我知道这个问题已有3年了,但是它在许多Google搜索中排在首位,因此我认为它需要一个专门解决原始问题的答案。
基本上要执行此操作,您需要将代码更改为此:
@IBAction func openPreferences(sender: NSMenuItem) {
let wc = SettingsViewController(windowNibName: NSNib.Name("NameOfXib"))
wc.showWindow(nil)
}
答案 1 :(得分:0)
我决定在NSView中使用自定义Storyboard而不是Xib。所以这里是我的代码。
AppDelegate.swift:
class AppDelegate: NSObject, NSApplicationDelegate {
lazy var settingsController: NSWindowController? =
NSStoryboard(name: "SettingsStoryboard", bundle: nil).instantiateControllerWithIdentifier("SettingsWC")
as? NSWindowController
@IBAction func openPreferences(sender: NSMenuItem) {
if let controller = settingsController { controller.showWindow(self) }
}
}