Contents

beginSheet(content:completionHandler:)

Presents a SwiftUI view as a sheet on the receiving NSWindow.

Declaration

@discardableResult @MainActor @preconcurrency func beginSheet<V>(@ContentBuilder content: () -> V, completionHandler: (() -> Void)? = nil) -> NSWindow.HostingSheetRepresentation<V> where V : View

Parameters

  • content:

    The SwiftUI view to present in a sheet.

  • completionHandler:

    An optional completion handler that is called when the sheet is dismissed for any reason.

Return Value

A discardable NSWindow.HostingSheetRepresentation instance.

Discussion

The presented view supports the same features as when used in the sheet(isPresented:onDismiss:content:) or sheet(item:onDismiss:content:) view modifier, such as:

parentWindow.beginSheet {
    NameADogSheet(dog: observableDog)
}

struct NameADogSheet: View {
    var dog: Dog
    @Environment(\.dismiss) private var dismiss
    @State private var name: String = ""

    var body: some View {
        Form {
            TextField("Who's a good dog?", text: $name)
        }
        .formStyle(.grouped)
        .toolbar {
            ToolbarItem(placement: .cancellationAction) {
                Button("Cancel") {
                    dismiss()
                }
            }
            ToolbarItem(placement: .confirmationAction) {
                Button("Suggest Name") {
                    dog.name = name
                    dismiss()
                }
                .disabled(name.isEmpty)
            }
        }
    }
}

The returned NSWindow.HostingSheetRepresentation can be ignored unless the sheet needs to be manipulated from an AppKit context, such as changing the root view or programmatically changing the sheet.

See Also

Managing Sheets