1

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

使用具有.swipeActions调用.confirmationDialog确认对话框的列表不显示。

参见示例:

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

4

1 回答 1

0

配置错误:

视图修饰符.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()
    }
}

于 2021-06-13T11:22:52.457 回答