Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想使按钮处于非活动状态,直到满足SwiftUI条件_Swift_Swiftui - Fatal编程技术网

我想使按钮处于非活动状态,直到满足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

我想使按钮处于非活动状态,直到满足条件(在我的复选框中为非活动状态)

如何在SwiftUI中执行此操作

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//