【问题标题】:SwiftUI: confirmationDialog disappearing after one secondSwiftUI:确认对话框在一秒后消失
【发布时间】:2023-01-28 03:42:05
【问题描述】:

请帮助我摆脱这个问题:

重现我的问题的步骤:

  1. 点击“编辑我的名字”按钮

  2. 在 .sheet 中,点击 TextField,然后在仍然显示键盘的情况下,一直向下滚动

  3. 点击按钮“删除名称”

  4. 这是问题所在:

    confirmationDialog只出现一秒钟,然后 消失,不给用户任何机会(或少于一秒钟的机会) 点击确认对话框的按钮之一!

    这是我的代码:

    内容视图.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()
        }
    }
    

    编辑视图.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")))
        }
    }
    

【问题讨论】:

    标签: ios swift xcode swiftui


    【解决方案1】:

    如果您将 .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?")
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      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
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-24
        • 1970-01-01
        • 2020-03-19
        • 2017-05-28
        相关资源
        最近更新 更多