1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SwiftUIで書いたMacAppで自ウィンドウを閉じる方法

Posted at

その方法をネットで調べたところ、モーダルを閉じるときと同じでした。
下記コードで実現できます。

struct ContentView: View {
    @Environment(\.dismiss) var dismiss
    var body: some View {
        Button("Close") {
            dismiss()
        }
    }
}

これで、NSApplicationDelegateapplicationShouldTerminateAfterLastWindowClosedメソッドが呼ばれます。このメソッドでtrueを返せば、アプリを終了できます。

NSApplication.shared.terminate(nil)を呼ぶとapplicationShouldTerminateAfterLastWindowClosedメソッドが呼ばれずにアプリが終了します。

どちらの方法でアプリを終了させても
NSApplicationDelegateapplicationWillTerminateメソッドが呼ばれるため、アプリの後処理はここに書くのがよさそうです。


@Environmentでやれることが沢山あるので、覚えられません。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?