问题描述
关于 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()
}
}