SwiftUI Settings scene (Preferences): how call action when closed by user (click on red button or Cmd-W)

Hi,

I thought this should be quite easy and maybe I only have tomatoes on my eyes, but I cannot find out how to call an action when the user clicks the red button or use CMD-W to close the Preferences window (= Settings Scene).

I use Core Data. In the Preferences, many data structures, which define my system, are changed. I learned, that you shouldn't save too often to avoid performance problems, so now I want to save the Core Data context when the user closes the Preferences window.

I tried .onDisappear, onChange of focus etc. but this didn't work.

How can I define an action?

Any hints are welcome :-)

Replies

Hi h725, just stumbled upon the same issue. Did you find any solution?

Use onDismiss view modifier in your SettingsView.