Presenting alerts and confirmation dialogs from identifiable data in SwiftUI
Learn how to present alerts and confirmation dialogs using optional identifiable items instead of Boolean state.
24 Aug 2026 · 4 min read
One possibility to control SwiftUI alerts and confirmation dialogs is with a Boolean binding. Starting with iOS 27, item-based alerts and confirmation dialogs are supported and provide an alternative by using an optional identifiable value as both the presentation state and its data.
Let's look how to use it.

Presenting an alert with a Boolean binding
Imagine we want to ask for confirmation before deleting a task. A Boolean-based implementation could look like this:
struct TasksView: View {@State private var taskToDelete: Task?@State private var isShowingDeleteAlert = falsevar body: some View {List(tasks) { task inButton("Delete \(task.title)") {taskToDelete = taskisShowingDeleteAlert = true}}.alert("Delete task?", isPresented: $isShowingDeleteAlert) {Button("Delete", role: .destructive) {if let taskToDelete {delete(taskToDelete)}}Button("Cancel", role: .cancel) {}} message: {if let taskToDelete {Text("Are you sure you want to delete \(taskToDelete.title)?")}}}}
This requires two pieces of state: one to decide whether to present the alert and another to hold the task. Because taskToDelete is optional, we also need to unwrap it in both the action and the message. These values represent the same event, but they can get out of sync.
Presenting an alert from an identifiable item
When the data conforms to Identifiable, we can instead pass a binding to the optional item directly to the alert modifier:
struct Task: Identifiable {let id = UUID()let title: String}struct TasksView: View {@State private var taskToDelete: Task?var body: some View {List(tasks) { task inButton("Delete \(task.title)") {taskToDelete = task}}.alert("Delete task?", item: $taskToDelete) { task inButton("Delete", role: .destructive) {delete(task)}Button("Cancel", role: .cancel) {}} message: { task inText("Are you sure you want to delete \(task.title)?")}}}
Setting taskToDelete to a value presents the alert. SwiftUI passes that value into the actions and message closures, so we can use the selected task without optional unwrapping. When the alert is dismissed, SwiftUI sets the binding back to nil.
We now have a single source of truth:
- taskToDelete == nil means that the alert is not presented.
- A task stored in taskToDelete means that the alert is presented for that task.
This makes item-based presentation especially useful for alerts whose title, message, or actions depend on selected data.
Using the same approach with confirmation dialogs
Confirmation dialogs support the same item-binding pattern. We only need to replace the alert modifier:
.confirmationDialog("Delete task?",item: $taskToDelete,titleVisibility: .visible) { task inButton("Delete", role: .destructive) {delete(task)}} message: { task inText("Are you sure you want to delete \(task.title)?")}
The item-based overloads for alerts and confirmation dialogs are available starting with iOS 27. For earlier versions, we still need to use the Boolean-based APIs.



Newsletter
Like to support my work?
Say hi
Related tags
Articles with related topics
Latest articles and tips



