Please help me get rid of this problem:
Steps to reproduce my problem:
-
Tap "Edit My Name" Button
-
Inside .sheet, tap on the TextField and then with keyboard still shown, scroll all the way down
-
Tap on the Button "Delete Name"
-
Here is the problem:
confirmationDialog appears only for one second, and then
disappears, not giving the user any chance (or less than one second chance)
to tap one of the confirmationDialog’s Buttons!
Here’s my code:
ContentView.swift
import SwiftUI
struct ContentView: View {
@State private var myName = "Joe"
@State private var isEditingName = false
var body: some View {
Text("My name is: (myName)")
Button("Edit My Name") {
isEditingName = true
}
.padding()
.sheet(isPresented: $isEditingName) {
EditView(name: $myName)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
EditView.swift
import SwiftUI
struct EditView: View {
@Binding var name: String
@State private var isShowingConfirmationDialog = false
var body: some View {
Form {
Section {
TextField("Name", text: $name)
}
Section {
VStack {
ForEach(0..<50, id: .self) { number in
Text("(number)")
}
}
}
Section {
deleteNameWithConfirmationDialog
}
}
}
private var deleteNameWithConfirmationDialog: some View {
Button("Delete Name", role: .destructive) {
isShowingConfirmationDialog = true
}
.confirmationDialog("Are you sure you want to delete name?", isPresented: $isShowingConfirmationDialog) {
Button("Delete Name", role: .destructive) {
name = ""
}
Button("Cancel", role: .cancel) { }
} message: {
Text("Are you sure you want to delte name?")
}
}
}
struct EditView_Previews: PreviewProvider {
static var previews: some View {
EditView(name: .constant(String("Joe")))
}
}
2
Answers
It works if you move the
.confirmationDialogue
out of theForm
:If you want to keep
.confirmationDialog
inside of theForm
, or if you are using.confirmationDialog
to manage deleting items within aList
, you can also avoid the immediate dismissal by excluding the.destructive
role from the deleteButton in.swipeActions
.