---
title: "alert(_:item:actions:)"
framework: swiftui
role: symbol
role_heading: Instance Method
path: "swiftui/view/alert(_:item:actions:)"
---

# alert(_:item:actions:)

Presents an alert using the given data to produce the alert’s content and a text view as a title.

## Declaration

```swift
nonisolated func alert<A, T>(_ title: Text, item data: Binding<T?>, @ContentBuilder actions: (T) -> A) -> some View where A : View

```

## Parameters

- `title`: The title of the alert.
- `data`: A binding to optional source of truth for the alert. The system presents the alert when the binding’s value is non-nil. When the user presses or taps one of the alert’s actions, the system sets this value to nil and dismisses. The system passes the contents to the modifier’s closures. You use this data to populate the fields of an alert that you create that the system displays to the user.
- `actions`: A doc://com.apple.SwiftUI/documentation/SwiftUI/ContentBuilder returning the alert’s actions given the currently available data.

## Discussion

Discussion For the alert to appear, data must not be nil. The data should not change after the presentation occurs. Any changes that you make after the presentation occurs are ignored. Use this method when you need to populate the fields of an alert with content from a data source. The example below shows a custom data source, SaveDetails, that provides data to populate the alert: struct SaveDetails: Identifiable {     let name: String     let error: String     let id = UUID() }

struct SaveButton: View {     @State private var details: SaveDetails?     let alertTitle: String = "Save failed."

var body: some View {         Button("Save") {             details = model.save()         }         .alert(             Text(alertTitle),             item: $details         ) { details in             Button(role: .destructive) {                 // Handle the deletion.             } label: {                 Text("Delete \(details.name)")             }             Button("Retry") {                 // Handle the retry action.             }         }     } } All actions in an alert dismiss the alert after the action runs. The default button is shown with greater prominence. You can influence the default button by assigning it the defaultAction keyboard shortcut. The system may reorder the buttons based on their role and prominence. If no actions are present, the system includes a standard “OK” action. No default cancel action is provided. If you want to show a cancel action, use a button with a role of cancel. On iOS, tvOS, and watchOS, alerts only support controls with labels that are Text. Passing any other type of view results in the content being omitted.

## See Also

### Presenting an alert

- [AlertScene](swiftui/alertscene.md)
- [alert(_:isPresented:actions:)](swiftui/view/alert(_:ispresented:actions:).md)
- [alert(_:isPresented:presenting:actions:)](swiftui/view/alert(_:ispresented:presenting:actions:).md)
- [alert(error:actions:)](swiftui/view/alert(error:actions:).md)
- [alert(isPresented:error:actions:)](swiftui/view/alert(ispresented:error:actions:).md)
- [alert(_:isPresented:actions:message:)](swiftui/view/alert(_:ispresented:actions:message:).md)
- [alert(_:isPresented:presenting:actions:message:)](swiftui/view/alert(_:ispresented:presenting:actions:message:).md)
- [alert(_:item:actions:message:)](swiftui/view/alert(_:item:actions:message:).md)
- [alert(error:actions:message:)](swiftui/view/alert(error:actions:message:).md)
- [alert(isPresented:error:actions:message:)](swiftui/view/alert(ispresented:error:actions:message:).md)
