.confirmationDialog внутри .swipeActions не работает, 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()
    }
}

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

Другие вопросы по тегам