Logo for tanaschita.com

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.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE

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 = false
var body: some View {
List(tasks) { task in
Button("Delete \(task.title)") {
taskToDelete = task
isShowingDeleteAlert = 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 in
Button("Delete \(task.title)") {
taskToDelete = task
}
}
.alert("Delete task?", item: $taskToDelete) { task in
Button("Delete", role: .destructive) {
delete(task)
}
Button("Cancel", role: .cancel) {}
} message: { task in
Text("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:

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 in
Button("Delete", role: .destructive) {
delete(task)
}
} message: { task in
Text("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.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

property wrappers

swiftui

swift

ios

Managing focus in SwiftUI with FocusState

Learn how to observe, move, and remove focus in SwiftUI views.

10 Aug 2026 · 4 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum