skip to content

In SwiftUI, how do you show an alert or confirmation dialog that carries data, such as the contact being deleted or a failed save?

level: middleimportance: should knowfreq 55%

answer

  1. the dialog acts on captured data
  2. presenting: plus a Bool
  3. roles: destructive and cancel
  4. Xcode 27 adds item and error forms

basics

~20 s

Pass the data through presenting: on confirmationDialog or alert: it appears only when the Bool is true and the data is non-nil, and hands that data to the actions. The iOS 27 SDK adds item: and error: forms driven by one optional.

solid answer

~40 s

For a deletion, `confirmationDialog(_:isPresented:titleVisibility:presenting:actions:)` (iOS 15) shows only when `isPresented` is `true` **and** the data is non-nil, and passes that contact into the `actions` closure, so the destructive button deletes the contact that was captured, not whatever is selected now. Buttons carry roles: `.destructive` for "Delete", `.cancel`, which replaces the dialog's standard dismiss action. For a failed save, `alert(_:isPresented:presenting:actions:message:)` works the same way. Every action dismisses the presentation after it runs, the system may reorder buttons by role, and on iOS the buttons must have `Text` labels. The iOS 27 SDK adds `alert(_:item:actions:)`, `confirmationDialog(_:item:titleVisibility:actions:)` and `alert(error:actions:)`: one optional binding presents the dialog and is set back to `nil` when an action is tapped.

code

swift · 23 lines
swift
struct ContactDetail: View {
    let name: String
    @State private var isConfirming = false
    @State private var target: String?

    var body: some View {
        Button("Delete Contact", role: .destructive) {
            target = name
            isConfirming = true
        }
        .confirmationDialog(
            "Delete this contact?",
            isPresented: $isConfirming,
            titleVisibility: .visible,
            presenting: target
        ) { doomed in
            Button("Delete \(doomed)", role: .destructive) {
                // delete `doomed`, the value captured at presentation
            }
            Button("Cancel", role: .cancel) {}
        }
    }
}

go deeper

for a junior

Recall the two modifiers: alert for reporting, confirmationDialog for confirming, each driven by a Bool, with Button roles .destructive and .cancel.

for a middle

Explain the presenting: contract: Bool true and data non-nil, data handed to the closures, later changes ignored, and every action dismissing after it runs.

for a senior

Show you avoid acting on the wrong record: capture the target in the presentation, and know the iOS 27 SDK item and error forms and how their availability reads.

for a principal

Decide how a codebase surfaces errors consistently: one error-presentation pattern per screen, LocalizedError types that write user-facing titles, and when a dialog should become a screen.

## Two system dialogs, two jobs SwiftUI gives you two modifiers for short, system-styled interruptions: | | `alert` | `confirmationDialog` | |---|---|---| | Purpose | report something and offer a few responses | confirm an action the person just started | | Contacts example | "The contact could not be saved" with Retry | "Delete Ada?" with a destructive Delete | | Title | a required parameter | shown or hidden through `titleVisibility` (`.automatic`, `.visible`, `.hidden`) | | Default extra action | an "OK" button if you supply no actions | a standard dismiss action | | Regular size class on iOS | the standard alert appearance | shown as a popover that a tap outside dismisses | Both are declared exactly like sheets: a modifier on a view, driven by state you own. ## Why the presenting: parameter exists The naive version stores the contact to delete in one `@State` property and a Bool in another, then reads the property inside the button action. The `presenting:` forms make the data part of the presentation instead: - `confirmationDialog(_:isPresented:titleVisibility:presenting:actions:)` and `alert(_:isPresented:presenting:actions:message:)` take `data: T?`. - The documentation says that for the interface to appear, **both** `isPresented` must be `true` **and** the data must be non-nil. - The data is passed into the `actions` and `message` closures, so the "Delete" button acts on the value that was presented. - The documentation adds that the data should not change after presentation; changes made afterwards are ignored. That last point is the useful one in review: whatever the list does while the dialog is up, the dialog still shows and acts on the contact it was presented with. ## The rules SwiftUI applies to actions - **Every action dismisses** the alert or dialog after the action runs. You never write "set the Bool to false" inside a button. - **Roles** come from `ButtonRole`: `.destructive` and `.cancel` since iOS 15, and `.confirm` and `.close` from iOS 26. The system may reorder buttons based on role and prominence. - **Cancel in a dialog**: a confirmation dialog includes a standard dismiss action; a button with the `.cancel` role takes its place, and its action need not dismiss anything itself. - **Alerts without actions** get a standard "OK". Alerts get no automatic cancel button, so add a `.cancel` button when you want one. - **Labels**: on iOS, alert and dialog buttons support only `Text` labels; other views are omitted. An alert's message supports only unstyled text. ## The item and error forms in the iOS 27 SDK Apple's "SwiftUI updates" page lists, among the June 2026 additions, alerts and confirmation dialogs that present from an optional data item or error: 1. `alert(_:item:actions:)` and `alert(_:item:actions:message:)` 2. `confirmationDialog(_:item:titleVisibility:actions:)` and its `message:` variant 3. `alert(error:actions:)` and `alert(error:actions:message:)` Each takes a `Binding` to an optional. The documentation for the alert item and error forms says the system presents when the value is non-nil and **sets it to `nil`** when the person taps an action; the interface builds the dialog's item form the same way, as a thin wrapper over the `presenting:` form. For `alert(error:actions:)` with a `LocalizedError`, the error's `errorDescription` becomes the title. On availability, read the declarations rather than assume: Apple's documentation pages list the item-based forms and the `LocalizedError` form of `alert(error:actions:)` from iOS 15, while the interface marks the overload that accepts any Swift `Error` as iOS 27. ## Deprecated shapes you will still meet - `alert(isPresented:content:)` and `alert(item:content:)` return the old `Alert` struct. The SDK marks them for deprecation and points to `alert(_:isPresented:presenting:actions:)`. - `ActionSheet` is the old confirmation shape, likewise marked for deprecation in favour of `confirmationDialog`. ## The contacts flow, end to end ```swift struct SaveFailure: LocalizedError { var errorDescription: String? { "The contact could not be saved." } } struct ContactActions: View { let name: String @State private var pendingDeletion: String? @State private var saveError: SaveFailure? var body: some View { Button("Delete Contact", role: .destructive) { pendingDeletion = name } .confirmationDialog("Delete this contact?", item: $pendingDeletion, titleVisibility: .visible) { doomed in Button("Delete \(doomed)", role: .destructive) { /* delete */ } Button("Cancel", role: .cancel) {} } .alert(error: $saveError) { Button("OK") {} } } } ``` The dialog acts on `doomed`, the captured name; the alert's title comes from `errorDescription`; both bindings return to `nil` when an action is tapped. In review, three questions catch most dialog bugs: does the destructive action act on data captured at presentation, is there exactly one cancel path, and does a failed save leave the screen's state intact so that Retry has something to retry?

  • Why pass the contact through presenting: instead of reading a selection property in the button action?
    Because the dialog then acts on the value it was presented with. The documentation says the data is passed to the action and message closures and that changes after presentation are ignored, so a list refresh or a new selection while the dialog is up cannot redirect the destructive action to a different contact.
  • How does a confirmation dialog look on iPad compared with iPhone?
    Apple's documentation notes that in regular size classes on iOS the system renders a confirmation dialog as a popover, which the person dismisses by tapping outside it, rather than showing the standard dismiss action. On iPhone it appears as the familiar sheet of buttons with a dismiss action, which a `.cancel` button replaces.
  • What is different about the item: and error: forms added in the iOS 27 SDK?
    They need one optional binding instead of a Bool plus data. The dialog or alert presents when the value is non-nil, receives the unwrapped value in its closures, and SwiftUI sets the binding back to `nil` when an action is tapped. `alert(error:actions:)` with a `LocalizedError` also takes its title from `errorDescription`.

  • A confirmation dialog appears as soon as the Bool is true, even with nil data.
  • Each alert button must set the presentation Bool to false itself.
  • A dialog's .cancel button adds a second cancel next to the system one.
  • Alert buttons on iOS can use any view, such as an Image with a Label.
  • Changing the presented data while the dialog is up updates its buttons.

Assumes the iOS 27 SDK in Xcode 27, which adds the item: and error: alert and confirmationDialog forms; the alert(error:) overload taking any Swift Error is marked iOS 27.