Skip to content

Instantly share code, notes, and snippets.

@zentrope
Last active June 19, 2019 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zentrope/72411dd3382c24c7da9846389d067c0a to your computer and use it in GitHub Desktop.
Save zentrope/72411dd3382c24c7da9846389d067c0a to your computer and use it in GitHub Desktop.
Open a window programmatically
//
// 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