gpt4 book ai didi

swiftui - 解雇表 SwiftUI

转载 作者:行者123 更新时间:2023-12-04 16:33:16 29 4
gpt4 key购买 nike

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

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

但是点击时返回按钮什么也不做。当显示模态时,控制台中会出现以下内容:

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



如果您强行打开 present你发现它是 nil
我如何解雇 .sheet以编程方式?

最佳答案

使用 presentationMode来自 @Environment .

Beta 6


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

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

关于swiftui - 解雇表 SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57233276/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com