0

I'm trying to load a NSWindow from an NSViewController on OS X and i'm doing the following:

private lazy var discoverable: DiscoverableWindow = {
     return DiscoverableWindow.instanceFromNib()
} ()

The static method instanceFromNib() is defined as below:

class func instanceFromNib() -> DiscoverableWindow {
    var instance = DiscoverableWindow()
    var objects: NSArray?
    NSBundle.mainBundle().loadNibNamed("DiscoverableWindow", owner: instance, topLevelObjects: &objects)
    return instance
}

I'm using the window to show from my NSViewController:

    NSApp.beginSheet(self.discoverable, modalForWindow: NSApplication.sharedApplication().mainWindow!, modalDelegate: nil, didEndSelector: nil, contextInfo: nil)

However, when I load it I see the following:

enter image description here

Is there something i'm doing incorrectly? Why is the NSWindow blank? I read the following on this:

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html

Cocoa - loadNibNamed:owner:topLevelObjects: from loaded bundle

Cocoa: NSApp beginSheet sets the application delegate?

Community
  • 1
  • 1
KVISH
  • 12,923
  • 17
  • 86
  • 162

1 Answers1

1

Why don't you make a window controller to handle the DiscoverableWindow?

Create a subclass of NSWindowController, make sure "Also create xib file for user interface" is selected. Configure your window in the DiscoverableWindowController xib, uncheck "Visible At Launch" on the properties inspector for the window.

enter image description here

Then, in your ViewController:

@IBAction func showSheet(sender: NSButton) {
    let discoverableWC = DiscoverableWindowController(windowNibName: "DiscoverableWindowController")
    view.window?.beginSheet(discoverableWC.window!, completionHandler: nil)
}

Generally, each window in your app should be managed by its own window controller, let the window controller handle the nib loading and instantiation for you.

Download the sample project here.

Guilherme Rambo
  • 2,018
  • 17
  • 19
  • This works if you want to show DiscoverableWindow as a sheet. But, DiscoverableWindowController doesn't receive UI events like mouse clicks. I have this project https://github.com/Nordin-010/OSXModalView, which doesn't react on button click on the modal view. Maybe you know how to fix that? – AndaluZ Mar 07 '19 at 11:13