Use @Binding
and @State
struct CheckBoxView: View {
@Binding var isChecked: Bool //<-- Here
func toggle() {isChecked = !isChecked}
var body: some View {
Button(action: toggle){
HStack{
Image(systemName: isChecked ? "checkmark.square": "square")
}
}
}
}
struct ContentView: View {
@State private var isActivate: Bool = false //<- Here
var body: some View {
VStack {
HStack {
CheckBoxView(isChecked: $isActivate)
Text("Activate the checkbox")
}
.padding()
Button(action: {
// ...
}) {
Text(isActivate ? "Activate" : "Disable")
}
.disabled(!isActivate)
.frame(width: 100, height: 50, alignment: .center)
.foregroundColor(.white)
.background(Color.orange)
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…