Search code examples
cocoanswindowappkitnswindowcontroller

Programmatic window creation in -[NSWindowController loadWindow] not working


I'm trying to programmatically create a NSWindowController and its associated window. To do so, I override the window controller's loadWindow and create the window controller in my app delegate:

class MainWindowController: NSWindowController {
    override func loadWindow() {
        print("loadWindow")
        self.window = NSWindow(contentRect: NSMakeRect(100, 100, 100, 100), styleMask: [.titled, .resizable, .miniaturizable, .closable], backing: .buffered, defer: false)
    }
}

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    var mwc: MainWindowController!

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        self.mwc = MainWindowController(window: nil)
        self.mwc.showWindow(nil)
    }
}

When I run this, I don't see the window, nor does "loadWindow" get printed to the console so it's not even getting called. Any ideas?


Solution

  • as @Willeke points out, loadWindow will only be called for nib

    but since you're calling showWindow, you could override that

    import Cocoa
    
    class MainWindowController: NSWindowController {
        override func loadWindow() {
            print("loadWindow")
        }
    
        override func showWindow(_ sender: Any?) {
            print("showWindow")
            self.window = NSWindow(contentRect: NSMakeRect(100, 100, 100, 100), styleMask: [.titled, .resizable, .miniaturizable, .closable], backing: .buffered, defer: false)
            window?.makeKeyAndOrderFront(sender)
        }
    }
    
    @main
    class AppDelegate: NSObject, NSApplicationDelegate {
        var mwc: MainWindowController!
    
        func applicationDidFinishLaunching(_ aNotification: Notification) {
            self.mwc = MainWindowController(window: nil)
            self.mwc.showWindow(nil)
        }
    
        static func main() {
            let app = NSApplication.shared
            let delegate = AppDelegate()
            app.delegate = delegate
            app.setActivationPolicy(.regular)
            app.activate(ignoringOtherApps: true)
            app.run()
        }
    }