我有一个按钮,当按下该按钮时会显示一个弹出窗口,并且在弹出窗口上是一个应该用来关闭弹出窗口本身的按钮。
我不确定如何在这里使用@Binding变量(如果我正确地假设那是我应该在不同结构之间进行通信的方式)
struct TESTSTSTSTS: View {
@State var showPopUp = false
var body: some View {
VStack {
Button(action: {
self.showPopUp = true
}) {
Text("Show PopUp Button")
}
Spacer()
if self.showPopUp == true {
PopUp()
}
}
}
}
struct PopUp: View {
var body: some View {
ZStack {
Color.orange
Button(action: {
//Unsure what code to use here.
}) {
Text("Hide PopUp Button")
}
}.frame(width: 300, height: 500, alignment: .center)
}
}
答案 0 :(得分:0)
@Binding确实可以解决此问题。
它是这样的:
struct ContentView : View {
@State var showPopUp = false
var body: some View {
VStack {
Button(action: {
self.showPopUp = true
}) {
Text("Show PopUp Button")
}
Spacer()
if self.showPopUp == true {
PopUp(showPopUp: $showPopUp)
}
}
}
}
struct PopUp: View {
@Binding var showPopUp: Bool
var body: some View {
ZStack {
Color.orange
Button(action: {
self.showPopUp.toggle()
}) {
Text("Hide PopUp Button")
}
}.frame(width: 300, height: 500, alignment: .center)
}
}