我目前有一个List
位于模态 ( .sheet
) 视图中的,我正在尝试将点击的值从它检索回前一个视图。我怎样才能做到这一点?
import SwiftUI
struct ContentView: View {
@State var showingModal: Bool = false
var body: some View {
VStack {
Button(action: {
self.showingModal.toggle()
}) {
Text("Tap me!")
}.sheet(isPresented: $showingModal, onDismiss: {
print("Dismissed")
}) {
ModalView(isShowing: self.$showingModal)
}
Text("Selected Item: ")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import SwiftUI
struct ModalView: View {
@State private var names = ["Foo", "Bar"]
@Binding var isShowing: Bool
var body: some View {
List(names, id: \.self) { name in
HStack {
Text("\(name)")
Spacer()
}
.contentShape(Rectangle())
.onTapGesture {
print(name)
self.isShowing = false
}
}
}
}
struct ModalView_Previews: PreviewProvider {
@State static var value = true
static var previews: some View {
ModalView(isShowing: self.$value)
}
}
在这个例子中,我想在Text
with上设置文本。"Selected item"