【问题标题】:.confirmationDialog inside of .swipeActions does not work, iOS 15.swipeActions 中的 .confirmationDialog 不起作用,iOS 15
【发布时间】:2021-08-29 09:32:33
【问题描述】:

关于 iOS 15、Xcode 13;我想知道这是一个错误、未正确实现还是计划中的非功能性功能......

如果列表包含调用.confirmationDialog.swipeActions,则不会显示确认对话框。

查看示例:

import SwiftUI

struct ContentView: View {
    
    @State private var confirmDelete = false
    
    var body: some View {
        NavigationView {
            List{
                ForEach(1..<10) {_ in
                    Cell()
                }

                .swipeActions(edge: .trailing) {
                    Button(role: .destructive) {
                        confirmDelete.toggle()
                    } label: {
                        Label("Delete", systemImage: "trash")
                    }

                    .confirmationDialog("Remove this?", isPresented: $confirmDelete) {
                        Button(role: .destructive) {
                            print("Removed!")
                        } label: {
                            Text("Yes, Remove this")
                        }
                    }
                }
            }
        }
    }
}

struct Cell: View {
    var body: some View {
        Text("Hello")
            .padding()
    }
}

【问题讨论】:

    标签: swiftui xcode13 ios15


    【解决方案1】:

    配置错误:

    需要将视图修饰符.confirmationDialog 添加到位于.swipeActions 视图修饰符外部的视图中。正确配置后,如下图所示:

    import SwiftUI
    
    struct ContentView: View {
        
        @State private var confirmDelete = false
        
        var body: some View {
            NavigationView {
                List{
                    ForEach(1..<10) {_ in
                        Cell()
                    }
                    .swipeActions(edge: .trailing) {
                        Button(role: .destructive) {
                            confirmDelete.toggle()
                        } label: {
                            Label("Delete", systemImage: "trash")
                        }
                    }
                    //move outside the scope of the .swipeActions view modifier:
                    .confirmationDialog("Remove this?", isPresented: $confirmDelete) {
                        Button(role: .destructive) {
                            print("Removed!")
                        } label: {
                            Text("Yes, Remove this")
                        }
                    }
                }
            }
        }
    }
    
    struct Cell: View {
        var body: some View {
            Text("Hello")
                .padding()
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-17
      • 2021-12-05
      • 2021-10-27
      • 2022-07-22
      • 2021-11-25
      • 2021-11-22
      • 2021-09-15
      • 1970-01-01
      相关资源
      最近更新 更多