SwiftUI工作表闭包捕获列表语法



如何将捕获列表添加到SwiftUI.sheet(content:(闭包?

我在SwiftUI中有一张表,在content:closure中,我检查一个可选的值,以确定要显示哪个视图。第一次运行时,即使我事先设置好了,值也总是为零。我提交了一份错误报告,苹果公司表示,如果我引用了闭包捕获列表中的变量,那么它将按预期工作。我是SwiftUI的新手,还没能找到正确的语法。语法是什么?

struct ContentView: View {
@State var presentButtonTwoDetail: Bool = false

@State var seletedIndex: Int? = nil
var body: some View {
Text("Hello")
.sheet(isPresented: $presentButtonTwoDetail) {
selectedIndex = nil
} content: {
{
[selectedIndex] // This syntax won't compile
() -> View in
if let theIndex = selectedIndex {
DetailView(selectedIndex: theIndex)
} else {
// This gets called on the first run only even when the `selectedIndex` is not nil.
DetailView(selectedIndex: 0)
}
}
}
}
}

编译。

struct ContentView: View {

@State var presentButtonTwoDetail: Bool = false

@State var selectedIndex: Int? = nil

var body: some View {
Text("Hello")
.sheet(isPresented: $presentButtonTwoDetail) {
selectedIndex = nil
} content: { [selectedIndex] in
if let theIndex = selectedIndex {
DetailView(selectedIndex: theIndex)
} else {
DetailView(selectedIndex: 0)
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新