首页 > 解决方案 > Xcode Beta 6 中的 SwiftUI 模式?

问题描述

以前在 SwiftUI (Xcode Beta 5) 中,模态的工作方式如下:

struct ContentView: View {

    @State var modalIsPresented: Bool = false

    var body: some View {

        Button(action: {

            self.modalIsPresented = true

        }) {

            Text("Show modal")

        }

        .sheet(isPresented: $modalIsPresented, content: {

            ModalView()

        })

    }

}

struct ModalView: View {

    @Environment(\.presentationMode) var presentationMode

    var body: some View {

        Button(action: {

            self.presentationMode.value.dismiss()

        }) {

            Text("Hide modal")

        }

    }

}

但是现在在 Xcode Beta 6 中,我找不到关闭模式的方法。不再有 的value属性presentationMode,其他属性似乎也没有我可以使用的有用方法。

如何在 Xcode Beta 6 中关闭 SwiftUI 模式?

标签: iosswiftxcodeswiftuixcode11

解决方案


在 Xcode Beta 6 中使用WrappedValue而不是value似乎可以工作:

self.presentationMode.wrappedValue.dismiss()

推荐阅读