마지막 창이 닫힐 때 SwiftUI 응용 프로그램 닫기 [중복]

Jan 16 2021

applicationShouldTerminateAfterLastWindowClosedAppDelegate 함수 와 유사하게 사용자가 마지막 창을 닫을 때 macOS SwiftUI 응용 프로그램을 닫을 수 있습니까?

func applicationShouldTerminateAfterLastWindowClosed(NSApplication) -> Bool

답변

2 DuncanGroenewald Jan 16 2021 at 04:20

여기서 답을 찾았습니다 https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app

AppDelegate에 대한 클래스 만들기

import Foundation
import AppKit

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        return true
    }
}

SwiftUI App 클래스에 속성 래퍼 추가

import SwiftUI

@main
struct SwiftUIApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .frame(minWidth: 300, idealWidth: 300, maxWidth: .infinity, minHeight: 300, idealHeight: 300, maxHeight: .infinity)
    
        }
    }
}