我想使按钮处于非活动状态,直到满足SwiftUI条件
我想使按钮处于非活动状态,直到满足条件(在我的复选框中为非活动状态) 如何在SwiftUI中执行此操作我想使按钮处于非活动状态,直到满足SwiftUI条件,swift,swiftui,Swift,Swiftui,我想使按钮处于非活动状态,直到满足条件(在我的复选框中为非活动状态) 如何在SwiftUI中执行此操作 struct CheckBoxView: View { @State var isChecked:Bool = false func toggle() {isChecked = !isChecked} var body: some View { Button(action: toggle){ HSta
struct CheckBoxView: View {
@State var isChecked:Bool = false
func toggle() {isChecked = !isChecked}
var body: some View {
Button(action: toggle){
HStack{
Image(systemName: isChecked ? "checkmark.square": "square")
}
}
}
}
struct SwiftUIViewTest: View {
var cb = CheckBoxView()
var body: some View {
VStack {
HStack {
cb
Text("Activate the checkbox")
}
.padding()
Button(action: {
// ...
}) {
Text("Activate")
}
.frame(width: 100, height: 50, alignment: .center)
.foregroundColor(.white)
.background(Color.orange)
}
}
}
我如何引用按钮?
在哪里编写逻辑更好呢?使用
@Binding
和@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)
}
}
}
struct CheckBoxView:View{
@已检查绑定变量:Bool//