sindresorhus / Settings

⚙ Add a settings window to your macOS app in minutes
MIT License
1.45k stars 100 forks source link

manage close window event #48

Closed manang closed 4 years ago

manang commented 4 years ago

Hi, I want to manage the close event (click on red button on the top left). in the app delegate I catch the function func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool

in this event I want to call a function in the panels. Is there a way to manage this? Thanks

sindresorhus commented 4 years ago

This question is not really related specifically to Preferences. You would handle it exactly how you would handle it with a normal window. I would recommend asking on Stack Overflow instead. It's your NSWindowController. You can listen to the close event and do something then.

manang commented 4 years ago

no, the problem is in the preferences case. I mean, In app delegate I can catch the event, I want to undestand how I can call a method inside the panel of the preferences. Thank you very much

sindresorhus commented 4 years ago

There's no difference from calling a method inside your panel as there is to call a method inside your own window or view. A preference pane is just a normal view. You could use NotificationCenter, a delegate, a callback, a weak reference, etc.

manang commented 4 years ago

sorry if I ask you, I'm new in swift in general for example in your example I want to call a method test inside GeneralPreferenceViewController when the main window catch applicationShouldTerminateAfterLastWindowClosed event. how can I manage it? Thank you for your help

sindresorhus commented 4 years ago
import Cocoa
import Preferences

@NSApplicationMain
final class AppDelegate: NSObject, NSApplicationDelegate {
    lazy var generalPreferencesViewController = GeneralPreferenceViewController()

    lazy var preferencesWindowController = PreferencesWindowController(
        preferencePanes: [
            generalPreferencesViewController,
            AdvancedPreferenceViewController()
        ]
    )
}

(Untested)

You can then call generalPreferencesViewController.someMethod() where you need to.