SwiftUI:确认对话框在一秒后消失

xjreopfe  于 2023-01-29  发布在  Swift
关注(0)|答案(2)|浏览(179)

请帮我摆脱这个问题:
重现问题的步骤:
1.点击"编辑我的姓名"按钮
1.在. sheet中,点击TextField,然后在键盘仍显示的情况下,一直向下滚动
1.点击按钮"删除姓名"
1.* * 问题是**

    • confirmationDialog仅出现一秒钟,然后消失,不给用户任何机会(或少于一秒钟的机会)点击confirmationDialog的按钮之一!**

下面是我的代码:
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")))
    }
}
ct3nt3jp

ct3nt3jp1#

如果您将.confirmationDialogue移出Form,则可以正常工作:

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 {
                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 delete name?")
        }
    }
}
kyxcudwk

kyxcudwk2#

如果您希望将.confirmationDialog保留在Form中,或者如果您正在使用.confirmationDialog来管理List中的删除项,您还可以通过从.swipeActions中的deleteButton中排除.destructive角色来避免立即解除。

var body: some View {
    List { // this could also be a Form
        ForEach(listItems) { item in 
            ItemRow(item: item) // confirmationDialog is in ItemRow
        }.swipeActions { 
            Button(action: { /* deleteMethodHere */ }) { 
                Image(systemName: "trash") 
            }.tint(.red) // to keep the swipe button red
        }
    }
}

相关问题