Last active
June 19, 2019 19:08
-
-
Save zentrope/72411dd3382c24c7da9846389d067c0a to your computer and use it in GitHub Desktop.
Open a window programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.swift | |
// | |
import Cocoa | |
@NSApplicationMain | |
class AppDelegate: NSObject, NSApplicationDelegate { | |
private var preferencesWindow = NSWindowController() | |
func applicationDidFinishLaunching(_ aNotification: Notification) { | |
// Insert code here to initialize your application | |
} | |
func applicationWillTerminate(_ aNotification: Notification) { | |
// Insert code here to tear down your application | |
} | |
@IBAction func openPreferencesWindow(_ sender: NSMenuItem) { | |
if let _ = preferencesWindow.window { | |
preferencesWindow.showWindow(self) | |
return | |
} | |
let content = PreferencesViewController() | |
let window = NSWindow(contentRect: .zero, styleMask: [.titled, .closable], backing: .buffered, defer: true) | |
window.contentViewController = content | |
window.windowController = preferencesWindow | |
window.title = "Preferences" | |
preferencesWindow.window = window | |
window.makeKeyAndOrderFront(self) | |
window.layoutIfNeeded() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment