.swipeActions 内的 .confirmationDialog 不起作用,iOS 15

问题描述

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

解决方法

配置错误:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...