OS: MacOS language: Swift 4+
What I want
I want to show a simple custom modal view from my main window. This custom modal view I created in a separate .xib file with belonging NSWindowController class.
In Xcode I added a new file --> CocoaClass --> Subclass of NSWindowController --> Checkmarked "Also create XIB file for user interface"
I than added a button and generated an action handler that simply prints text on the debug console. This to test whether the button press is correctly handled or not.
To show the modal view from my main window I have the following code executed when a button has pressed:
let modal = SimpleModal(windowNibName: "SimpleModal")
self.view.window?.beginSheet(modal.window!, completionHandler: { response in
print("Finished device selection")
})
This does show the view, but not as a modal view. It simply shows the view next to my main window and the modal view also doesn't react on mouse click events, when I click on the button in the modal view.
I don't know how to get this right, so if you know how to do this, please answer me.
NOTE:
When modal view shows up, the main window should be unresponsive until the user dismisses the modal view. It's like moving the focus from main window to the modal view.
The source of my demo project: OSXModalView
You have to remove the Visible At Launch
flag in the attribute inspector of your window object in SimpleModal.xib
. Otherwise your code works.