Instance Method インスタンスメソッド

alert(item:content:)

Presents an alert to the user. アラートをユーザに提示します。

Declaration 宣言

func alert<Item>(item: Binding<Item?>, content: (Item) -> Alert) -> some View where Item : Identifiable

Parameters パラメータ

item

A binding to an optional source of truth for the alert. アラートに対するある随意の信頼できる情報源へのあるバインディング。 if item is non-nil, the system passes the contents to the modifier’s closure. You use this content to populate the fields of an alert that you create that the system displays to the user. If item changes, the system dismisses the currently displayed alert and replaces it with a new one using the same process.

content

A closure returning the alert to present. 提示するアラートを返しているあるクロージャ。

Discussion 議論

Use this method when you need to show an alert that contains information from a binding to an optional data source that you provide. The example below shows a custom data source FileInfo whose properties configure the alert’s message field:


struct FileInfo: Identifiable {
    var id: String { name }
    let name: String
    let fileType: UTType
}


struct ConfirmImportAlert: View {
    @State var alertDetails: FileInfo?
    var body: some View {
        Button("Show Alert") {
            alertDetails = FileInfo(name: "MyImageFile.png",
                                    fileType: .png)
        }
        .alert(item: $alertDetails) { details in
            Alert(title: Text("Import Complete"),
                  message: Text("""
                    Imported \(details.name) \n File
                    type: \(details.fileType.description).
                    """),
                  dismissButton: .default(Text("Dismiss")))
        }
    }
}

An alert showing information from a data source that describes the