I want to open a preference pane in my Cocoa app when an user taps the Preferences...
menu or typing ⌘+,. So I connected from the preferences menu item to the window controller in storyboard as a show
segue.
However, while this opens the preference pane when an user taps Preferences...
, if the user taps the menu item again before closing the pane, another pane is going to be launched and displayed on the screen.
I want to have only one preference pane in my screen but how can I do that? I want to set it up only on storyboard and avoid coding that makes the nest of nest of boring menu items...
In order to prevent the windows from being launched multiple times, you select Single
from the Window Controller's Attribute Inspector on Storyboard.
The default value is Multiple
, causing the infinite windows to be launched.
Alternatively, if you want to use the code, here is something I will do in AppDelegate.swift
.
extension AppDelegate: NSSeguePerforming {
func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if identifier == OpenPreferencesIdentifier {
let windows = NSApp.windows
for window in windows {
if window.windowController?.className == MyPreferencesWindowController.className() {
return false
}
}
}
return true
}
}
Also do NOT forget to set the appropriate segue identifier on Storyboard.