关闭 sheet SwiftUI

Dismiss sheet SwiftUI

我正在尝试为我的模式 sheet 实现一个关闭按钮,如下所示:

struct TestView: View {
    @Environment(\.isPresented) var present
    var body: some View {
        Button("return") {
            self.present?.value = false
        }
    }
}

struct DataTest : View {
    @State var showModal: Bool = false

    var modal: some View {
        TestView()
    }
    var body: some View {
        Button("Present") {
            self.showModal = true
        }.sheet(isPresented: $showModal) {
            self.modal
        }
    }
}

但是 return 按钮在点击时没有任何反应。显示模态时,控制台中会出现以下内容:

[WindowServer] display_timer_callback: unexpected state (now:5fbd2efe5da4 < expected:5fbd2ff58e89)

如果你强制展开 present 你会发现它是 nil

如何以编程方式关闭 .sheet

对我来说,beta 4 打破了这种使用关闭按钮的方法——使用环境变量 isPresented。这是我现在做的事情:

struct ContentView: View {
    @State var showingModal = false

    var body: some View {
        Button(action: {
           self.showingModal.toggle()
        }) {
           Text("Show Modal")
        }
        .sheet(
            isPresented: $showingModal,
            content: { ModalPopup(showingModal: self.$showingModal) }
        )
    }
}

并且在您的模态视图中:

struct ModalPopup : View {
    @Binding var showingModal:Bool

    var body: some View {
        Button(action: {
            self.showingModal = false
        }) {
            Text("Dismiss").frame(height: 60)
        }
    }
}

使用 @Environment 中的 presentationMode

Beta 6

struct SomeView: View {
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        VStack {
            Text("Ohay!")
            Button("Close") {
                self.presentationMode.wrappedValue.dismiss()
            }
        }
    }
}

Apple 建议(在 WWDC 2020 Data Essentials in SwiftUI 中)为此使用 @State@Binding。他们还将 isEditorPresented 布尔值和 sheet 的数据放在使用 @State 声明的相同 EditorConfig 结构中,因此它可以被改变,如下所示:

import SwiftUI

struct Item: Identifiable {
    let id = UUID()
    let title: String
}

struct EditorConfig {
    var isEditorPresented = false
    var title = ""
    var needsSave = false
    
    mutating func present() {
        isEditorPresented = true
        title = ""
        needsSave = false
    }
    
    mutating func dismiss(save: Bool = false) {
        isEditorPresented = false
        needsSave = save
    }
}

struct ContentView: View {
    
    @State var items = [Item]()
    @State private var editorConfig = EditorConfig()
    
    var body: some View {
        NavigationView {
            Form {
                ForEach(items) { item in
                    Text(item.title)
                }
            }
            .navigationTitle("Items")
            .toolbar {
                ToolbarItem(placement: .primaryAction) {
                    Button(action: presentEditor) {
                        Label("Add Item", systemImage: "plus")
                    }
                }
            }
            .sheet(isPresented: $editorConfig.isEditorPresented, onDismiss: {
                if(editorConfig.needsSave) {
                    items.append(Item(title: editorConfig.title))
                }
            }) {
                EditorView(editorConfig: $editorConfig)
            }
        }
    }
    
    func presentEditor() {
        editorConfig.present()
    }
}

struct EditorView: View {
    @Binding var editorConfig: EditorConfig
    var body: some View {
        NavigationView {
            Form {
                TextField("Title", text:$editorConfig.title)
            }
            .toolbar {
                ToolbarItem(placement: .confirmationAction) {
                    Button(action: save) {
                        Text("Save")
                    }
                    .disabled(editorConfig.title.count == 0)
                }
                ToolbarItem(placement: .cancellationAction) {
                    Button(action: dismiss) {
                        Text("Dismiss")
                    }
                }
            }
        }
    }
    
    func save() {
        editorConfig.dismiss(save: true)
    }
    
    func dismiss() {
        editorConfig.dismiss()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(items: [Item(title: "Banana"), Item(title: "Orange")])
    }
}

iOS15

从 iOS 15 开始我们可以使用 DismissAction 可以访问为 @Environment(\.dismiss).

不用再用presentationMode.wrappedValue.dismiss().

struct SheetView: View {
    @Environment(\.dismiss) var dismiss

    var body: some View {
        NavigationView {
            Text("Sheet")
                .toolbar {
                    Button("Done") {
                        dismiss()
                    }
                }
        }
    }
}