Open window from menu

Multi tool use
Open window from menu
How can I open new window from my Xib file in Swift2? I've created new NSWindowController + xib, but I can't find how to open it?
@IBAction func openPreferences(sender: NSMenuItem) {
let wc = SettingsViewController()
//...
}
2 Answers
2
I've decided to use custom Storyboard instead of Xib with NSView. So here my code.
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) }
}
}
I know this question is three years old but it's at the top of a number of Google searches so I think it needs an answer that specifically addresses the original question.
Basically to get this working you need to change the code to this:
@IBAction func openPreferences(sender: NSMenuItem) {
let wc = SettingsViewController(windowNibName: NSNib.Name("NameOfXib"))
wc.showWindow(nil)
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
you have to call showWindow on the new windowController. Also mind that you need to have strong reference to your windowController (if not the object will disappear at the end of method). You can have weak one if you have link from XIB (there is an option not to display window at launch).
– Marek H
Oct 2 '15 at 19:17