我有一个按钮,当按下PopUp时,它会显示一个PopUp,而在PopUp上,这个按钮应该是关闭PopUp本身的按钮。
我不确定如何在这里使用@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)
}
}发布于 2020-05-22 17:13:25
@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)
}
}https://stackoverflow.com/questions/61960337
复制相似问题