Let’s us a selectable List
as follows:
import SwiftUI
struct ContentView: View {
@State private var elements: Set<UUID> = [.init(), .init(), .init(), .init(), .init()]
@State private var selection: Set<UUID> = []
@State private var isConfirmationDialogPresented: Bool = false
var body: some View {
NavigationStack {
List(
elements.sorted(),
id: .self,
selection: $selection,
rowContent: { Text(String(describing: $0)).lineLimit(1) }
)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
EditButton()
}
ToolbarItem(placement: .bottomBar) {
Button(
"Delete (selection.count) element(s)",
role: .destructive,
action: { isConfirmationDialogPresented = true }
)
.disabled(selection.isEmpty)
.confirmationDialog(
"Delete (selection.count) elements(s)",
isPresented: $isConfirmationDialogPresented,
actions: {
Button("Delete (selection.count) element(s)", action: {
elements = elements.subtracting(selection)
})
}
)
}
}
}
}
}
When tapping the Button
to display the confirmation dialog, the selectable List
is reset. How to prevent this?
Note: moving the .confirmationDialog
to List
causes the same behavior.
Tested with Xcode 16 beta 4 • iOS 18.